github.com/coreos/mantle@v0.13.0/lang/maps/sorted_test.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package maps
    16  
    17  import (
    18  	"sort"
    19  	"testing"
    20  
    21  	"github.com/coreos/mantle/lang/natsort"
    22  )
    23  
    24  var (
    25  	// some random goo
    26  	testKeys = []string{
    27  		"100uquie",
    28  		"10ocheiv",
    29  		"1hiexieh",
    30  		"cheuzash",
    31  		"ohbohmop",
    32  		"oobeecoh",
    33  		"ohxadupu",
    34  		"yuilohsh",
    35  		"oongoojo",
    36  		"mielutao",
    37  		"iriecier",
    38  		"eisheiba",
    39  		"ahsoogup",
    40  		"aabeevie",
    41  		"aeyaebek",
    42  		"kaibahgh",
    43  	}
    44  )
    45  
    46  func TestBadKeys(t *testing.T) {
    47  	defer func() {
    48  		r := recover()
    49  		if r == nil {
    50  			t.Errorf("Keys did not panic")
    51  		} else if r != "maps: keys must be strings" {
    52  			panic(r)
    53  		}
    54  	}()
    55  	Keys(map[int]int{})
    56  }
    57  
    58  func TestSortedKeys(t *testing.T) {
    59  	testMap := make(map[string]bool)
    60  	for _, k := range testKeys {
    61  		testMap[k] = true
    62  	}
    63  
    64  	// test is pointless if map iterates in-order by random chance
    65  	mapKeys := make([]string, 0, len(testMap))
    66  	for k, _ := range testMap {
    67  		mapKeys = append(mapKeys, k)
    68  	}
    69  	if sort.StringsAreSorted(mapKeys) {
    70  		t.Skip("map is already iterating in order!")
    71  	}
    72  
    73  	sortedKeys := SortedKeys(testMap)
    74  	if !sort.StringsAreSorted(sortedKeys) {
    75  		t.Error("SortedKeys did not sort the keys!")
    76  	}
    77  
    78  	if len(sortedKeys) != len(testKeys) {
    79  		t.Errorf("SortedKeys returned %d keys, not %d",
    80  			len(sortedKeys), len(testKeys))
    81  	}
    82  }
    83  
    84  func TestNaturalKeys(t *testing.T) {
    85  	testMap := make(map[string]bool)
    86  	for _, k := range testKeys {
    87  		testMap[k] = true
    88  	}
    89  
    90  	// test is pointless if map iterates in-order by random chance
    91  	mapKeys := make([]string, 0, len(testMap))
    92  	for k, _ := range testMap {
    93  		mapKeys = append(mapKeys, k)
    94  	}
    95  	if natsort.StringsAreSorted(mapKeys) {
    96  		t.Skip("map is already iterating in order!")
    97  	}
    98  
    99  	sortedKeys := NaturalKeys(testMap)
   100  	if !natsort.StringsAreSorted(sortedKeys) {
   101  		t.Error("SortedKeys did not sort the keys!")
   102  	}
   103  
   104  	if len(sortedKeys) != len(testKeys) {
   105  		t.Errorf("SortedKeys returned %d keys, not %d",
   106  			len(sortedKeys), len(testKeys))
   107  	}
   108  }