int32_test.gno

1.95 Kb ยท 73 lines
 1package int32
 2
 3import "testing"
 4
 5func TestOne(t *testing.T) {
 6	ttt := []struct {
 7		exp string
 8		res int
 9	}{
10		{"1", 1},
11		{"--1", 1},
12		{"1+2", 3},
13		{"-1+2", 1},
14		{"-(1+2)", -3},
15		{"-(1+2)*5", -15},
16		{"-(1+2)*5/3", -5},
17		{"1+(-(1+2)*5/3)", -4},
18		{"3^4", 3 ^ 4},
19		{"8%2", 8 % 2},
20		{"8%3", 8 % 3},
21		{"8|3", 8 | 3},
22		{"10%2", 0},
23		{"(4    + 3)/2-1+11*15", (4+3)/2 - 1 + 11*15},
24		{
25			"(30099>>10^30099>>11)%5*((30099>>14&3^30099>>15&1)+1)*30099%99 + ((3 + (30099 >> 14 & 3) - (30099 >> 16 & 1)) / 3 * 30099 % 99 & 64)",
26			(30099>>10^30099>>11)%5*((30099>>14&3^30099>>15&1)+1)*30099%99 + ((3 + (30099 >> 14 & 3) - (30099 >> 16 & 1)) / 3 * 30099 % 99 & 64),
27		},
28		{
29			"(1023850>>10^1023850>>11)%5*((1023850>>14&3^1023850>>15&1)+1)*1023850%99 + ((3 + (1023850 >> 14 & 3) - (1023850 >> 16 & 1)) / 3 * 1023850 % 99 & 64)",
30			(1023850>>10^1023850>>11)%5*((1023850>>14&3^1023850>>15&1)+1)*1023850%99 + ((3 + (1023850 >> 14 & 3) - (1023850 >> 16 & 1)) / 3 * 1023850 % 99 & 64),
31		},
32		{"((0000+1)*0000)", 0},
33	}
34	for _, tc := range ttt {
35		t.Run(tc.exp, func(t *testing.T) {
36			exp, err := Parse(tc.exp)
37			if err != nil {
38				t.Errorf("%s:\n%s", tc.exp, err.Error())
39			} else {
40				res, errEval := Eval(exp, nil)
41				if errEval != nil {
42					t.Errorf("eval error: %s", errEval.Error())
43				} else if res != tc.res {
44					t.Errorf("%s:\nexpected %d, got %d", tc.exp, tc.res, res)
45				}
46			}
47		})
48	}
49}
50
51func TestVariables(t *testing.T) {
52	fn := func(x, y int) int {
53		return 1 + ((x*3+1)*(x*2))>>y + 1
54	}
55	expr := "1 + ((x*3+1)*(x*2))>>y + 1"
56	exp, err := Parse(expr)
57	if err != nil {
58		t.Errorf("could not parse: %s", err.Error())
59	}
60	variables := make(map[string]int)
61	for i := 0; i < 10; i++ {
62		variables["x"] = i
63		variables["y"] = 2
64		res, errEval := Eval(exp, variables)
65		if errEval != nil {
66			t.Errorf("could not evaluate: %s", err.Error())
67		}
68		expected := fn(variables["x"], variables["y"])
69		if res != expected {
70			t.Errorf("expected: %d, actual: %d", expected, res)
71		}
72	}
73}