github.com/aristanetworks/gomap@v0.0.0-20240103001659-f6b0e31fb1a7/iter_test.go (about)

     1  // Modifications copyright (c) Arista Networks, Inc. 2024
     2  // Underlying
     3  // Copyright 2014 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  //go:build goexperiment.rangefunc
     8  
     9  package gomap
    10  
    11  import (
    12  	"hash/maphash"
    13  	"maps"
    14  	"testing"
    15  )
    16  
    17  func TestRangeFuncs(t *testing.T) {
    18  	m := New(
    19  		func(a, b string) bool { return a == b },
    20  		maphash.String,
    21  		KeyElem[string, string]{"Avenue", "AVE"},
    22  		KeyElem[string, string]{"Street", "ST"},
    23  		KeyElem[string, string]{"Court", "CT"},
    24  	)
    25  
    26  	t.Run("All", func(t *testing.T) {
    27  		exp := map[string]string{
    28  			"Avenue": "AVE",
    29  			"Street": "ST",
    30  			"Court":  "CT",
    31  		}
    32  		got := make(map[string]string)
    33  		for k, v := range m.All() {
    34  			got[k] = v
    35  		}
    36  		if !maps.Equal(exp, got) {
    37  			t.Errorf("expected: %v got: %v", exp, got)
    38  		}
    39  	})
    40  
    41  	t.Run("Keys", func(t *testing.T) {
    42  		exp := map[string]struct{}{
    43  			"Avenue": struct{}{},
    44  			"Street": struct{}{},
    45  			"Court":  struct{}{},
    46  		}
    47  		got := make(map[string]struct{})
    48  		for k := range m.Keys() {
    49  			got[k] = struct{}{}
    50  		}
    51  		if !maps.Equal(exp, got) {
    52  			t.Errorf("expected: %v got: %v", exp, got)
    53  		}
    54  	})
    55  
    56  	t.Run("Values", func(t *testing.T) {
    57  		exp := map[string]struct{}{
    58  			"AVE": struct{}{},
    59  			"ST":  struct{}{},
    60  			"CT":  struct{}{},
    61  		}
    62  		got := make(map[string]struct{})
    63  		for k := range m.Values() {
    64  			got[k] = struct{}{}
    65  		}
    66  		if !maps.Equal(exp, got) {
    67  			t.Errorf("expected: %v got: %v", exp, got)
    68  		}
    69  	})
    70  }