github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/pkg/sortedmap/sortedmap_test.go (about) 1 // Copyright 2018 the u-root 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 sortedmap 6 7 import ( 8 "testing" 9 ) 10 11 func TestInsert(t *testing.T) { 12 cases := []struct { 13 data map[int64]int64 14 keys []int64 15 }{ 16 {data: map[int64]int64{1: 2, 3: 4}, keys: []int64{1, 3}}, 17 {data: map[int64]int64{4: 0, 1: 0}, keys: []int64{1, 4}}, 18 } 19 20 for _, c := range cases { 21 m := NewMap() 22 23 for k, v := range c.data { 24 m.Insert(k, v) 25 } 26 27 // All expected entries exist 28 for k, e := range c.data { 29 v, ok := m.m[k] 30 if !ok { 31 t.Errorf("%d not found in %v", k, m.m) 32 } 33 if v != e { 34 t.Errorf("got %d want %d in %v", v, e, m.m) 35 } 36 } 37 38 if len(m.k) != len(c.keys) { 39 t.Errorf("Bad length, got %d, expected %d. %v vs %v", len(m.k), len(c.keys), m.k, c.keys) 40 } 41 42 // Key slice is correct 43 for i, e := range c.keys { 44 if m.k[i] != e { 45 t.Errorf("Got %v, expected %v", m.k, c.keys) 46 break 47 } 48 } 49 } 50 } 51 52 func TestDelete(t *testing.T) { 53 cases := []struct { 54 before Map 55 del []int64 56 after Map 57 }{ 58 { 59 before: Map{ 60 m: map[int64]int64{1: 0, 2: 0}, 61 k: sortedSlice{1, 2}, 62 }, 63 del: []int64{1}, 64 after: Map{ 65 m: map[int64]int64{2: 0}, 66 k: sortedSlice{2}, 67 }, 68 }, 69 { 70 before: Map{ 71 m: map[int64]int64{1: 0, 2: 0}, 72 k: sortedSlice{1, 2}, 73 }, 74 del: []int64{1, 2}, 75 after: Map{ 76 m: map[int64]int64{}, 77 k: sortedSlice{}, 78 }, 79 }, 80 } 81 82 for i := range cases { 83 for _, k := range cases[i].del { 84 cases[i].before.Delete(k) 85 } 86 87 // All expected entries exist 88 for k, e := range cases[i].after.m { 89 v, ok := cases[i].before.m[k] 90 if !ok { 91 t.Errorf("%d not found in %v", k, cases[i].before.m) 92 } 93 if v != e { 94 t.Errorf("got %d want %d in %v", v, e, cases[i].before.m) 95 } 96 } 97 98 if len(cases[i].before.k) != len(cases[i].after.k) { 99 t.Errorf("Bad length, got %d, expected %d. %v vs %v", len(cases[i].before.k), len(cases[i].after.k), cases[i].before.k, cases[i].after.k) 100 } 101 102 // Key slice is correct 103 for i, e := range cases[i].after.k { 104 if cases[i].before.k[i] != e { 105 t.Errorf("Got %v, expected %v", cases[i].before.k, cases[i].after.k) 106 break 107 } 108 } 109 } 110 } 111 112 func TestGet(t *testing.T) { 113 m := Map{ 114 m: map[int64]int64{2: 20, 4: 40}, 115 k: sortedSlice{2, 4}, 116 } 117 118 // Simple lookup 119 var v int64 120 var ok bool 121 if v, ok = m.Get(2); !ok { 122 t.Errorf("want ok got false for Get(2)") 123 } 124 if v != 20 { 125 t.Errorf("want 20 got %d for Get(2)", v) 126 } 127 128 // Non-existant key 129 if _, ok = m.Get(3); ok { 130 t.Errorf("want not ok got ok for Get(3)") 131 } 132 } 133 134 func TestNearestLessEqual(t *testing.T) { 135 m := Map{ 136 m: map[int64]int64{2: 20, 4: 40}, 137 k: sortedSlice{2, 4}, 138 } 139 140 // Nothing less than smallest 141 _, _, err := m.NearestLessEqual(1) 142 if err != ErrNoSuchKey { 143 t.Errorf("want err got nil for NLE(1)") 144 } 145 146 // Exact match 147 k, v, err := m.NearestLessEqual(2) 148 if err != nil { 149 t.Errorf("want nil got err for NLE(2): %v", err) 150 } 151 if k != 2 { 152 t.Errorf("bad key for NLE(2): want 2 got %d", k) 153 } 154 if v != 20 { 155 t.Errorf("bad value for NLE(2): want 20 got %d", k) 156 } 157 158 // One above 159 k, v, err = m.NearestLessEqual(3) 160 if err != nil { 161 t.Errorf("want nil got err for NLE(3): %v", err) 162 } 163 if k != 2 { 164 t.Errorf("bad key for NLE(3): want 2 got %d", k) 165 } 166 if v != 20 { 167 t.Errorf("bad value for NLE(3): want 20 got %d", k) 168 } 169 170 // Way above 171 k, v, err = m.NearestLessEqual(1000) 172 if err != nil { 173 t.Errorf("want nil got err for NLE(1000): %v", err) 174 } 175 if k != 4 { 176 t.Errorf("bad key for NLE(1000): want 4 got %d", k) 177 } 178 if v != 40 { 179 t.Errorf("bad value for NLE(1000): want 40 got %d", k) 180 } 181 } 182 183 func TestNearestGreaterEqual(t *testing.T) { 184 m := Map{ 185 m: map[int64]int64{2: 20, 4: 40}, 186 k: sortedSlice{2, 4}, 187 } 188 189 // Nothing bigger than biggest 190 _, _, err := m.NearestGreater(5) 191 if err != ErrNoSuchKey { 192 t.Errorf("want ErrNoSuchKey got %v for NG(5)", err) 193 } 194 195 // One below 196 k, v, err := m.NearestGreater(3) 197 if err != nil { 198 t.Errorf("want nil got err for NG(3): %v", err) 199 } 200 if k != 4 { 201 t.Errorf("bad key for NG(3): want 4 got %d", k) 202 } 203 if v != 40 { 204 t.Errorf("bad value for NG(3): want 40 got %d", k) 205 } 206 207 // Way below 208 k, v, err = m.NearestGreater(-1000) 209 if err != nil { 210 t.Errorf("want nil got err for NG(-1000): %v", err) 211 } 212 if k != 2 { 213 t.Errorf("bad key for NG(-1000): want 2 got %d", k) 214 } 215 if v != 20 { 216 t.Errorf("bad value for NG(-1000): want 20 got %d", k) 217 } 218 }