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