blob: 978f1e763c09f4d2ae5badbcc33a46dad0771af0 [file] [log] [blame]
Dan Willemsenbc60c3c2021-12-15 01:09:00 -08001// Copyright 2021 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8 "a"
9 "bytes"
10 "fmt"
11)
12
13func TestMap() {
14 m := a.New[[]byte, int](bytes.Compare)
15
16 if _, found := m.Find([]byte("a")); found {
17 panic(fmt.Sprintf("unexpectedly found %q in empty map", []byte("a")))
18 }
19
20 for _, c := range []int{'a', 'c', 'b'} {
21 if !m.Insert([]byte(string(c)), c) {
22 panic(fmt.Sprintf("key %q unexpectedly already present", []byte(string(c))))
23 }
24 }
25 if m.Insert([]byte("c"), 'x') {
26 panic(fmt.Sprintf("key %q unexpectedly not present", []byte("c")))
27 }
28
29 if v, found := m.Find([]byte("a")); !found {
30 panic(fmt.Sprintf("did not find %q", []byte("a")))
31 } else if v != 'a' {
32 panic(fmt.Sprintf("key %q returned wrong value %c, expected %c", []byte("a"), v, 'a'))
33 }
34 if v, found := m.Find([]byte("c")); !found {
35 panic(fmt.Sprintf("did not find %q", []byte("c")))
36 } else if v != 'x' {
37 panic(fmt.Sprintf("key %q returned wrong value %c, expected %c", []byte("c"), v, 'x'))
38 }
39
40 if _, found := m.Find([]byte("d")); found {
41 panic(fmt.Sprintf("unexpectedly found %q", []byte("d")))
42 }
43
44 gather := func(it *a.Iterator[[]byte, int]) []int {
45 var r []int
46 for {
47 _, v, ok := it.Next()
48 if !ok {
49 return r
50 }
51 r = append(r, v)
52 }
53 }
54 got := gather(m.Iterate())
55 want := []int{'a', 'b', 'x'}
56 if !a.SliceEqual(got, want) {
57 panic(fmt.Sprintf("Iterate returned %v, want %v", got, want))
58 }
59
60}
61
62func main() {
63 TestMap()
64}