Source file test/typeparam/orderedmapsimp.dir/main.go

     1  // 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  
     5  package main
     6  
     7  import (
     8  	"./a"
     9  	"bytes"
    10  	"fmt"
    11  )
    12  
    13  func 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  
    62  func main() {
    63  	TestMap()
    64  }
    65  

View as plain text