github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/omap/osmap/osmap_test.go (about)

     1  package osmap
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestStringKeyOMapInsertion(t *testing.T) {
     9  	wordForWord := New()
    10  	for _, word := range []string{"one", "Two", "THREE", "four", "Five"} {
    11  		wordForWord.Insert(strings.ToLower(word), word)
    12  	}
    13  	var words []string
    14  	fc := func(_, value string) {
    15  		words = append(words, value)
    16  	}
    17  	wordForWord.Do(fc)
    18  	actual, expected := strings.Join(words, ""), "FivefouroneTHREETwo"
    19  	if actual != expected {
    20  		t.Errorf("%q != %q", actual, expected)
    21  	}
    22  }
    23  
    24  func TestIntKeyOMapFind(t *testing.T) {
    25  	intMap := New()
    26  	for _, number := range []string{"9", "1", "8", "2", "7", "3", "6", "4", "5", "0"} {
    27  
    28  		intMap.Insert(number, "0"+number)
    29  	}
    30  	for _, number := range []string{"0", "1", "5", "8", "9"} {
    31  		value, found := intMap.Find(number)
    32  		if !found {
    33  			t.Errorf("failed to find %d", number)
    34  		}
    35  		actual, expected := value, "0"+number
    36  		if actual != expected {
    37  			t.Errorf("value is %d should be %d", actual, expected)
    38  		}
    39  	}
    40  	for _, number := range []string{"-1", "-21", "10", "11", "148"} {
    41  		_, found := intMap.Find(number)
    42  		if found {
    43  			t.Errorf("should not have found %d", number)
    44  		}
    45  	}
    46  }
    47  
    48  func TestIntKeyOMapDelete(t *testing.T) {
    49  	intMap := New()
    50  	for _, number := range []string{"9", "1", "8", "2", "7", "3", "6", "4", "5", "0"} {
    51  		intMap.Insert(number, "0"+number)
    52  	}
    53  	if intMap.Len() != 10 {
    54  		t.Errorf("map len %d should be 10", intMap.Len())
    55  	}
    56  	length := 9
    57  	for i, number := range []string{"0", "1", "5", "8", "9"} {
    58  		if deleted := intMap.Delete(number); !deleted {
    59  			t.Errorf("failed to delete %d", number)
    60  		}
    61  		if intMap.Len() != length-i {
    62  			t.Errorf("map len %d should be %d", intMap.Len(), length-i)
    63  		}
    64  	}
    65  	for _, number := range []string{"-1", "-21", "10", "11", "148"} {
    66  		if deleted := intMap.Delete(number); deleted {
    67  			t.Errorf("should not have deleted nonexistent %d", number)
    68  		}
    69  	}
    70  	if intMap.Len() != 5 {
    71  		t.Errorf("map len %d should be 5", intMap.Len())
    72  	}
    73  }