amuz.es/src/go/misc@v1.0.1/nums/digits_test.go (about) 1 package nums 2 3 import "testing" 4 5 func TestSplitSignFromInt64(t *testing.T) { 6 type args struct { 7 src int64 8 } 9 tests := []struct { 10 name string 11 args args 12 wantSign bool 13 wantDst uint64 14 }{ 15 { 16 "0", 17 args{0}, 18 false, 0, 19 }, 20 { 21 "-0", 22 args{-0}, 23 false, 0, 24 }, 25 { 26 "-223", 27 args{-223}, 28 true, 223, 29 }, 30 { 31 "112", 32 args{112}, 33 false, 112, 34 }, 35 } 36 for _, tt := range tests { 37 t.Run(tt.name, func(t *testing.T) { 38 gotSign, gotDst := SplitSignFromInt64(tt.args.src) 39 if gotSign != tt.wantSign { 40 t.Errorf("SplitSignFromInt64() gotSign = %v, want %v", gotSign, tt.wantSign) 41 } 42 if gotDst != tt.wantDst { 43 t.Errorf("SplitSignFromInt64() gotDst = %v, want %v", gotDst, tt.wantDst) 44 } 45 }) 46 } 47 } 48 49 func TestMergeSignFromUInt64(t *testing.T) { 50 type args struct { 51 sign bool 52 src uint64 53 } 54 tests := []struct { 55 name string 56 args args 57 wantDst int64 58 }{ 59 { 60 "0", 61 args{false, 0}, 62 0, 63 }, 64 { 65 "-0", 66 args{true, 0}, 67 0, 68 }, 69 { 70 "-223", 71 args{true, 223}, 72 -223, 73 }, 74 { 75 "112", 76 args{false, 112}, 77 112, 78 }, 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 if gotDst := MergeSignFromUInt64(tt.args.sign, tt.args.src); gotDst != tt.wantDst { 83 t.Errorf("MergeSignFromUInt64() = %v, want %v", gotDst, tt.wantDst) 84 } 85 }) 86 } 87 } 88 89 func TestFractionalStringify(t *testing.T) { 90 type args struct { 91 fractional int 92 fractionalLength int 93 showPoint bool 94 } 95 tests := []struct { 96 name string 97 args args 98 wantFormatted string 99 }{ 100 { 101 ".123", 102 args{ 103 123, 3, true, 104 }, 105 ".123", 106 }, 107 { 108 "length 0", 109 args{ 110 123, 0, true, 111 }, 112 "", 113 }, 114 { 115 ".1", 116 args{ 117 123, 1, true, 118 }, 119 ".1", 120 }, 121 { 122 ".12", 123 args{ 124 123, 2, true, 125 }, 126 ".12", 127 }, 128 { 129 ".0123", 130 args{ 131 123, 4, true, 132 }, 133 ".0123", 134 }, 135 { 136 ".01200", 137 args{ 138 1200, 5, true, 139 }, 140 ".01200", 141 }, 142 { 143 "123", 144 args{ 145 123, 3, false, 146 }, 147 "123", 148 }, 149 { 150 "length 0", 151 args{ 152 123, 0, false, 153 }, 154 "", 155 }, 156 { 157 "1", 158 args{ 159 123, 1, false, 160 }, 161 "1", 162 }, 163 { 164 "12", 165 args{ 166 123, 2, false, 167 }, 168 "12", 169 }, 170 { 171 "0123", 172 args{ 173 123, 4, false, 174 }, 175 "0123", 176 }, 177 { 178 "01200", 179 args{ 180 1200, 5, false, 181 }, 182 "01200", 183 }, 184 } 185 for _, tt := range tests { 186 t.Run(tt.name, func(t *testing.T) { 187 if gotFormatted := FractionalStringify(tt.args.fractional, tt.args.fractionalLength, tt.args.showPoint); gotFormatted != tt.wantFormatted { 188 t.Errorf("FractionalStringify() = %v, want %v", gotFormatted, tt.wantFormatted) 189 } else { 190 t.Logf("FractionalStringify() = %v", gotFormatted) 191 } 192 }) 193 } 194 }