github.com/hdt3213/godis@v1.2.9/datastruct/list/linked_test.go (about)

     1  package list
     2  
     3  import (
     4  	"github.com/hdt3213/godis/lib/utils"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func ToString(list *LinkedList) string {
    11  	arr := make([]string, list.size)
    12  	list.ForEach(func(i int, v interface{}) bool {
    13  		integer, _ := v.(int)
    14  		arr[i] = strconv.Itoa(integer)
    15  		return true
    16  	})
    17  	return "[" + strings.Join(arr, ", ") + "]"
    18  }
    19  
    20  func TestAdd(t *testing.T) {
    21  	list := Make()
    22  	for i := 0; i < 10; i++ {
    23  		list.Add(i)
    24  	}
    25  	list.ForEach(func(i int, v interface{}) bool {
    26  		intVal, _ := v.(int)
    27  		if intVal != i {
    28  			t.Error("add test fail: expected " + strconv.Itoa(i) + ", actual: " + strconv.Itoa(intVal))
    29  		}
    30  		return true
    31  	})
    32  }
    33  
    34  func BenchmarkLinkedList_Add(b *testing.B) {
    35  	list := Make()
    36  	for i := 0; i < pageSize*10; i++ {
    37  		list.Add(i)
    38  	}
    39  }
    40  
    41  func BenchmarkLinkedList_Range(b *testing.B) {
    42  	list := Make()
    43  	for i := 0; i < pageSize*10; i++ {
    44  		list.Add(i)
    45  	}
    46  	list.ForEach(func(i int, v interface{}) bool {
    47  		return true
    48  	})
    49  }
    50  
    51  func TestLinkedList_Contains(t *testing.T) {
    52  	list := Make(1, 2, 3, 4)
    53  	if !list.Contains(func(a interface{}) bool {
    54  		return a == 1
    55  	}) {
    56  		t.Error("expect true actual false")
    57  	}
    58  	if list.Contains(func(a interface{}) bool {
    59  		return a == -1
    60  	}) {
    61  		t.Error("expect false actual true")
    62  	}
    63  }
    64  
    65  func TestGet(t *testing.T) {
    66  	list := Make()
    67  	for i := 0; i < 10; i++ {
    68  		list.Add(i)
    69  	}
    70  	for i := 0; i < 10; i++ {
    71  		v := list.Get(i)
    72  		k, _ := v.(int)
    73  		if i != k {
    74  			t.Error("get test fail: expected " + strconv.Itoa(i) + ", actual: " + strconv.Itoa(k))
    75  		}
    76  	}
    77  }
    78  
    79  func TestRemove(t *testing.T) {
    80  	list := Make()
    81  	for i := 0; i < 10; i++ {
    82  		list.Add(i)
    83  	}
    84  	for i := 9; i >= 0; i-- {
    85  		list.Remove(i)
    86  		if i != list.Len() {
    87  			t.Error("remove test fail: expected size " + strconv.Itoa(i) + ", actual: " + strconv.Itoa(list.Len()))
    88  		}
    89  		list.ForEach(func(i int, v interface{}) bool {
    90  			intVal, _ := v.(int)
    91  			if intVal != i {
    92  				t.Error("remove test fail: expected " + strconv.Itoa(i) + ", actual: " + strconv.Itoa(intVal))
    93  			}
    94  			return true
    95  		})
    96  	}
    97  }
    98  
    99  func TestRemoveVal(t *testing.T) {
   100  	list := Make()
   101  	for i := 0; i < 10; i++ {
   102  		list.Add(i)
   103  		list.Add(i)
   104  	}
   105  	for index := 0; index < list.Len(); index++ {
   106  		list.RemoveAllByVal(func(a interface{}) bool {
   107  			return utils.Equals(a, index)
   108  		})
   109  		list.ForEach(func(i int, v interface{}) bool {
   110  			intVal, _ := v.(int)
   111  			if intVal == index {
   112  				t.Error("remove test fail: found  " + strconv.Itoa(index) + " at index: " + strconv.Itoa(i))
   113  			}
   114  			return true
   115  		})
   116  	}
   117  
   118  	list = Make()
   119  	for i := 0; i < 10; i++ {
   120  		list.Add(i)
   121  		list.Add(i)
   122  	}
   123  	for i := 0; i < 10; i++ {
   124  		list.RemoveByVal(func(a interface{}) bool {
   125  			return utils.Equals(a, i)
   126  		}, 1)
   127  	}
   128  	list.ForEach(func(i int, v interface{}) bool {
   129  		intVal, _ := v.(int)
   130  		if intVal != i {
   131  			t.Error("test fail: expected " + strconv.Itoa(i) + ", actual: " + strconv.Itoa(intVal))
   132  		}
   133  		return true
   134  	})
   135  	for i := 0; i < 10; i++ {
   136  		list.RemoveByVal(func(a interface{}) bool {
   137  			return utils.Equals(a, i)
   138  		}, 1)
   139  	}
   140  	if list.Len() != 0 {
   141  		t.Error("test fail: expected 0, actual: " + strconv.Itoa(list.Len()))
   142  	}
   143  
   144  	list = Make()
   145  	for i := 0; i < 10; i++ {
   146  		list.Add(i)
   147  		list.Add(i)
   148  	}
   149  	for i := 0; i < 10; i++ {
   150  		list.ReverseRemoveByVal(func(a interface{}) bool {
   151  			return a == i
   152  		}, 1)
   153  	}
   154  	list.ForEach(func(i int, v interface{}) bool {
   155  		intVal, _ := v.(int)
   156  		if intVal != i {
   157  			t.Error("test fail: expected " + strconv.Itoa(i) + ", actual: " + strconv.Itoa(intVal))
   158  		}
   159  		return true
   160  	})
   161  	for i := 0; i < 10; i++ {
   162  		list.ReverseRemoveByVal(func(a interface{}) bool {
   163  			return a == i
   164  		}, 1)
   165  	}
   166  	if list.Len() != 0 {
   167  		t.Error("test fail: expected 0, actual: " + strconv.Itoa(list.Len()))
   168  	}
   169  
   170  }
   171  
   172  func TestInsert(t *testing.T) {
   173  	list := Make()
   174  	for i := 0; i < 10; i++ {
   175  		list.Add(i)
   176  	}
   177  	for i := 0; i < 10; i++ {
   178  		list.Insert(i*2, i)
   179  
   180  		list.ForEach(func(j int, v interface{}) bool {
   181  			var expected int
   182  			if j < (i+1)*2 {
   183  				if j%2 == 0 {
   184  					expected = j / 2
   185  				} else {
   186  					expected = (j - 1) / 2
   187  				}
   188  			} else {
   189  				expected = j - i - 1
   190  			}
   191  			actual, _ := list.Get(j).(int)
   192  			if actual != expected {
   193  				t.Error("insert test fail: at i " + strconv.Itoa(i) + " expected " + strconv.Itoa(expected) + ", actual: " + strconv.Itoa(actual))
   194  			}
   195  			return true
   196  		})
   197  
   198  		for j := 0; j < list.Len(); j++ {
   199  			var expected int
   200  			if j < (i+1)*2 {
   201  				if j%2 == 0 {
   202  					expected = j / 2
   203  				} else {
   204  					expected = (j - 1) / 2
   205  				}
   206  			} else {
   207  				expected = j - i - 1
   208  			}
   209  			actual, _ := list.Get(j).(int)
   210  			if actual != expected {
   211  				t.Error("insert test fail: at i " + strconv.Itoa(i) + " expected " + strconv.Itoa(expected) + ", actual: " + strconv.Itoa(actual))
   212  			}
   213  		}
   214  
   215  	}
   216  }
   217  
   218  func TestRemoveLast(t *testing.T) {
   219  	list := Make()
   220  	for i := 0; i < 10; i++ {
   221  		list.Add(i)
   222  	}
   223  	for i := 9; i >= 0; i-- {
   224  		val := list.RemoveLast()
   225  		intVal, _ := val.(int)
   226  		if intVal != i {
   227  			t.Error("add test fail: expected " + strconv.Itoa(i) + ", actual: " + strconv.Itoa(intVal))
   228  		}
   229  	}
   230  }
   231  
   232  func TestRange(t *testing.T) {
   233  	list := Make()
   234  	size := 10
   235  	for i := 0; i < size; i++ {
   236  		list.Add(i)
   237  	}
   238  	for start := 0; start < size; start++ {
   239  		for stop := start; stop < size; stop++ {
   240  			slice := list.Range(start, stop)
   241  			if len(slice) != stop-start {
   242  				t.Error("expected " + strconv.Itoa(stop-start) + ", get: " + strconv.Itoa(len(slice)) +
   243  					", range: [" + strconv.Itoa(start) + "," + strconv.Itoa(stop) + "]")
   244  			}
   245  			sliceIndex := 0
   246  			for i := start; i < stop; i++ {
   247  				val := slice[sliceIndex]
   248  				intVal, _ := val.(int)
   249  				if intVal != i {
   250  					t.Error("expected " + strconv.Itoa(i) + ", get: " + strconv.Itoa(intVal) +
   251  						", range: [" + strconv.Itoa(start) + "," + strconv.Itoa(stop) + "]")
   252  				}
   253  				sliceIndex++
   254  			}
   255  		}
   256  	}
   257  }