github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/pkg/sortedmap/sortedslice_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 TestIntSliceInsert(t *testing.T) {
    12  	cases := []struct {
    13  		insert   []int64
    14  		expected []int64
    15  	}{
    16  		{insert: []int64{1, 2, 3}, expected: []int64{1, 2, 3}},
    17  		{insert: []int64{3, 2, 1}, expected: []int64{1, 2, 3}},
    18  		{insert: []int64{-4, 20, -10, 5}, expected: []int64{-10, -4, 5, 20}},
    19  	}
    20  
    21  	for _, c := range cases {
    22  		l := make(sortedSlice, 0)
    23  
    24  		for _, v := range c.insert {
    25  			l.Insert(v)
    26  		}
    27  
    28  		if len(l) != len(c.expected) {
    29  			t.Errorf("Bad length, got %d, expected %d. %v vs %v", len(l), len(c.expected), l, c.expected)
    30  		}
    31  
    32  		for i, e := range c.expected {
    33  			if l[i] != e {
    34  				t.Errorf("Got %v, expected %v", l, c.expected)
    35  				break
    36  			}
    37  		}
    38  	}
    39  }
    40  
    41  func TestIntSliceDelete(t *testing.T) {
    42  	cases := []struct {
    43  		slice    sortedSlice
    44  		del      []int64
    45  		expected []int64
    46  	}{
    47  		{slice: sortedSlice{1, 2, 3}, del: []int64{2}, expected: []int64{1, 3}},
    48  		{slice: sortedSlice{1, 2, 3}, del: []int64{3, 2, 1}, expected: []int64{}},
    49  		{slice: sortedSlice{-10, -4, 5, 20}, del: []int64{-4}, expected: []int64{-10, 5, 20}},
    50  	}
    51  
    52  	for _, c := range cases {
    53  		for _, v := range c.del {
    54  			c.slice.Delete(v)
    55  		}
    56  
    57  		if len(c.slice) != len(c.expected) {
    58  			t.Errorf("Bad length, got %d, expected %d. %v vs %v", len(c.slice), len(c.expected), c.slice, c.expected)
    59  		}
    60  
    61  		for i, e := range c.expected {
    62  			if c.slice[i] != e {
    63  				t.Errorf("Got %v, expected %v", c.slice, c.expected)
    64  				break
    65  			}
    66  		}
    67  	}
    68  }