刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package jnpf.elnFormula;
 
import jnpf.util.JsonUtil;
import org.junit.Test;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
 
public class FormulaSystemExamplesTest {
 
    private final FormulaParser parser = new FormulaParser();
    private final FormulaFunctionRegistry functions = new FormulaFunctionRegistry();
    private final FormulaTypeChecker typeChecker = new FormulaTypeChecker(functions);
    private final FormulaEvaluator evaluator = new FormulaEvaluator(functions);
 
    @Test
    public void evaluatesSystemExamplesAndStableFailures() throws Exception {
        List<Example> examples = loadExamples();
        assertEquals(25, examples.size());
 
        for (Example example : examples) {
            try {
                FormulaAst.Node ast = parser.parse(example.expression);
                Map<String, FormulaType> slots = slotTypes(example.slots);
                typeChecker.check(ast, slots);
                FormulaValue raw = evaluator.evaluate(ast, inputValues(example.inputs, slots));
                FormulaValue rounded = example.roundScale == null ? raw
                        : evaluator.round(raw, example.roundScale,
                        RoundingMode.valueOf(example.roundMode));
                if (example.errorCode != null) {
                    fail(example.name + " expected error " + example.errorCode);
                }
                assertEquals(example.name, example.expectedRaw, raw.toPlainString());
                assertEquals(example.name, example.expectedRounded == null
                        ? example.expectedRaw : example.expectedRounded, rounded.toPlainString());
            } catch (FormulaParser.ParseException ex) {
                assertError(example, ex.getCode());
            } catch (FormulaException ex) {
                assertError(example, ex.getCode());
            }
        }
    }
 
    @Test
    public void exposesStableFunctionCatalog() {
        assertEquals(asList("ABS", "POW", "SQRT", "MIN", "MAX", "IF", "COALESCE", "IS_BLANK",
                        "SUM", "MEAN", "STDDEV_S", "RSD", "ROUND", "ROUND_UP", "ROUND_DOWN",
                        "CONVERT", "LOOKUP_EXACT", "LOOKUP_LINEAR",
                        "LN", "LOG10", "LOG", "EXP", "NTH_ROOT",
                        "COUNT", "MEDIAN", "STDDEV_P", "VAR_S", "VAR_P", "GEOMEAN",
                        "WEIGHTED_MEAN", "SLOPE", "INTERCEPT", "CORREL", "LINEAR_PREDICT",
                        "ROUND_HALF_EVEN", "ROUND_SIG", "ROUND_SIG_HALF_EVEN"),
                functions.codes());
        for (FormulaFunction function : functions.catalog()) {
            assertNotNull(function.getChineseName());
            assertNotNull(function.getCategory());
            assertNotNull(function.getSignature());
            assertNotNull(function.getDescription());
            assertNotNull(function.getExample());
        }
    }
 
    private static void assertError(Example example, String actualCode) {
        if (example.errorCode == null) {
            fail(example.name + " unexpected error " + actualCode);
        }
        assertEquals(example.name, example.errorCode, actualCode);
    }
 
    private static Map<String, FormulaType> slotTypes(Map<String, String> values) {
        Map<String, FormulaType> result = new LinkedHashMap<>();
        for (Map.Entry<String, String> entry : values.entrySet()) {
            result.put(entry.getKey(), FormulaType.valueOf(entry.getValue()));
        }
        return result;
    }
 
    private static Map<String, FormulaValue> inputValues(Map<String, Object> values,
                                                          Map<String, FormulaType> slots) {
        Map<String, FormulaValue> result = new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : values.entrySet()) {
            FormulaType type = slots.get(entry.getKey());
            Object value = entry.getValue();
            if (value == null) {
                result.put(entry.getKey(), FormulaValue.nullValue());
            } else if (type == FormulaType.DECIMAL) {
                result.put(entry.getKey(), FormulaValue.decimal(value.toString()));
            } else if (type == FormulaType.DECIMAL_LIST) {
                List<FormulaValue> items = new ArrayList<>();
                for (Object item : (List<?>) value) {
                    items.add(FormulaValue.decimal(item.toString()));
                }
                result.put(entry.getKey(), FormulaValue.decimalList(items));
            } else if (type == FormulaType.BOOLEAN) {
                result.put(entry.getKey(), FormulaValue.bool(Boolean.parseBoolean(value.toString())));
            } else {
                result.put(entry.getKey(), FormulaValue.text(value.toString()));
            }
        }
        return result;
    }
 
    private static List<Example> loadExamples() throws IOException {
        InputStream input = FormulaSystemExamplesTest.class.getResourceAsStream(
                "/formula/system-examples.json");
        assertNotNull(input);
        try {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            int read;
            while ((read = input.read(buffer)) >= 0) {
                output.write(buffer, 0, read);
            }
            return JsonUtil.getJsonToList(new String(output.toByteArray(), StandardCharsets.UTF_8),
                    Example.class);
        } finally {
            input.close();
        }
    }
 
    private static List<String> asList(String... values) {
        List<String> result = new ArrayList<>();
        Collections.addAll(result, values);
        return result;
    }
 
    public static class Example {
        public String name;
        public String expression;
        public Map<String, String> slots = new LinkedHashMap<>();
        public Map<String, Object> inputs = new LinkedHashMap<>();
        public Integer roundScale;
        public String roundMode;
        public String expectedRaw;
        public String expectedRounded;
        public String errorCode;
    }
}