github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/cue/literal/num_test.go (about) 1 // Copyright 2020 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package literal 16 17 import ( 18 "fmt" 19 "math/big" 20 "strconv" 21 "testing" 22 23 "github.com/joomcode/cue/cue/token" 24 "github.com/google/go-cmp/cmp" 25 "github.com/google/go-cmp/cmp/cmpopts" 26 ) 27 28 func mkInt(i int) NumInfo { 29 return NumInfo{ 30 base: 10, 31 neg: i < 0, 32 buf: []byte(strconv.Itoa(i)), 33 } 34 } 35 36 func mkFloat(a string) NumInfo { 37 return NumInfo{ 38 base: 10, 39 buf: []byte(a), 40 neg: a[0] == '-', 41 isFloat: true, 42 } 43 } 44 45 func mkMul(i string, m Multiplier, base byte) NumInfo { 46 return NumInfo{ 47 base: base, 48 mul: m, 49 neg: i[0] == '-', 50 buf: []byte(i), 51 } 52 } 53 54 func TestNumbers(t *testing.T) { 55 // hk := newInt(testBase, newRepresentation(0, 10, true)).setInt64(100000) 56 testCases := []struct { 57 lit string 58 norm string 59 n NumInfo 60 }{ 61 {"0", "0", mkInt(0)}, 62 {"1", "1", mkInt(1)}, 63 {"-1", "-1", mkInt(-1)}, 64 {"100_000", "100000", NumInfo{UseSep: true, base: 10, buf: []byte("100000")}}, 65 {"1.", "1.", mkFloat("1.")}, 66 {"0.", "0.", mkFloat("0.")}, 67 {".0", "0.0", mkFloat("0.0")}, 68 {"012.34", "12.34", mkFloat("12.34")}, 69 {".01", "0.01", mkFloat("0.01")}, 70 {".01e2", "0.01e2", mkFloat("0.01e2")}, 71 {"0.", "0.", mkFloat("0.")}, 72 {"1K", "1000", mkMul("1", K, 10)}, 73 {".5K", "500", mkMul("0.5", K, 10)}, 74 {"1Mi", "1048576", mkMul("1", Mi, 10)}, 75 {"1.5Mi", "1572864", mkMul("1.5", Mi, 10)}, 76 // {"1.3Mi", &bottom{}}, // Cannot be accurately represented. 77 {"1.3G", "1300000000", mkMul("1.3", G, 10)}, 78 {"1.3e+20", "1.3e+20", mkFloat("1.3e+20")}, 79 {"1.3e20", "1.3e20", mkFloat("1.3e20")}, 80 {"1.3e-5", "1.3e-5", mkFloat("1.3e-5")}, 81 {".3e-1", "0.3e-1", mkFloat("0.3e-1")}, 82 {"0e-5", "0e-5", mkFloat("0e-5")}, 83 {"0E-5", "0e-5", mkFloat("0e-5")}, 84 {"5e-5", "5e-5", mkFloat("5e-5")}, 85 {"5E-5", "5e-5", mkFloat("5e-5")}, 86 {"0x1234", "4660", mkMul("1234", 0, 16)}, 87 {"0xABCD", "43981", mkMul("ABCD", 0, 16)}, 88 {"-0xABCD", "-43981", mkMul("-ABCD", 0, 16)}, 89 {"0b11001000", "200", mkMul("11001000", 0, 2)}, 90 {"0b1", "1", mkMul("1", 0, 2)}, 91 {"0o755", "493", mkMul("755", 0, 8)}, 92 {"0755", "493", mkMul("755", 0, 8)}, 93 } 94 n := NumInfo{} 95 for i, tc := range testCases { 96 t.Run(fmt.Sprintf("%d/%+q", i, tc.lit), func(t *testing.T) { 97 if err := ParseNum(tc.lit, &n); err != nil { 98 t.Fatal(err) 99 } 100 n.src = "" 101 n.p = 0 102 n.ch = 0 103 if !cmp.Equal(n, tc.n, diffOpts...) { 104 t.Error(cmp.Diff(n, tc.n, diffOpts...)) 105 t.Errorf("%#v, %#v\n", n, tc.n) 106 } 107 if n.String() != tc.norm { 108 t.Errorf("got %v; want %v", n.String(), tc.norm) 109 } 110 }) 111 } 112 } 113 114 var diffOpts = []cmp.Option{ 115 cmp.Comparer(func(x, y big.Rat) bool { 116 return x.String() == y.String() 117 }), 118 cmp.Comparer(func(x, y big.Int) bool { 119 return x.String() == y.String() 120 }), 121 cmp.AllowUnexported( 122 NumInfo{}, 123 ), 124 cmpopts.IgnoreUnexported( 125 token.Pos{}, 126 ), 127 cmpopts.EquateEmpty(), 128 } 129 130 func TestNumErrors(t *testing.T) { 131 testCases := []string{ 132 `0x`, 133 `0o`, 134 `0b`, 135 `0_`, 136 "0128", 137 "e+100", 138 ".p", 139 ``, 140 `"`, 141 `"a`, 142 `23.34e`, 143 `23.34e33pp`, 144 } 145 for _, tc := range testCases { 146 t.Run(fmt.Sprintf("%+q", tc), func(t *testing.T) { 147 n := &NumInfo{} 148 err := ParseNum(tc, n) 149 if err == nil { 150 t.Fatalf("expected error but found none") 151 } 152 }) 153 } 154 }