| 212 | } |
| 213 | |
| 214 | func TestHash(t *testing.T) { |
| 215 | tests := []struct { |
| 216 | c Constant |
| 217 | want uint64 |
| 218 | }{ |
| 219 | { |
| 220 | c: fooName, |
| 221 | want: hashBytes([]byte("/foo")), |
| 222 | }, |
| 223 | { |
| 224 | c: barString, |
| 225 | want: hashBytes([]byte(`bar`)), |
| 226 | }, |
| 227 | { |
| 228 | c: Number(num), |
| 229 | want: uint64(num), |
| 230 | }, |
| 231 | { |
| 232 | c: floatNumConstant, |
| 233 | want: math.Float64bits(floatNum), |
| 234 | }, |
| 235 | { |
| 236 | c: fooBarPair, |
| 237 | want: uint64(hashPair(&fooName, &barString, PairShape)), |
| 238 | }, |
| 239 | { |
| 240 | c: List([]Constant{fooName}), |
| 241 | want: uint64(hashPair(&fooName, &ListNil, ListShape)), |
| 242 | }, |
| 243 | { |
| 244 | c: MapCons(&fooName, &barString, &MapNil), |
| 245 | want: uint64(hashPair(&fooBarPair, &MapNil, MapShape)), |
| 246 | }, |
| 247 | { |
| 248 | c: StructCons(&fooName, &barString, &StructNil), |
| 249 | want: uint64(hashPair(&fooBarPair, &StructNil, StructShape)), |
| 250 | }, |
| 251 | } |
| 252 | for _, test := range tests { |
| 253 | if got := test.c.Hash(); got != test.want { |
| 254 | t.Errorf("(%v).Hash() expected %v got %v", test.c, test.want, got) |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | func TestAtomHash(t *testing.T) { |
| 260 | a := NewAtom("bar", String("foo")) |