github.com/mymmsc/gox@v1.3.33/util/linkedliststack/linkedliststack_test.go (about)

     1  // Copyright (c) 2015, Emir Pasic. 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 linkedliststack
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func TestStackPush(t *testing.T) {
    13  	stack := New()
    14  	if actualValue := stack.Empty(); actualValue != true {
    15  		t.Errorf("Got %v expected %v", actualValue, true)
    16  	}
    17  	stack.Push(1)
    18  	stack.Push(2)
    19  	stack.Push(3)
    20  
    21  	if actualValue := stack.Values(); actualValue[0].(int) != 3 || actualValue[1].(int) != 2 || actualValue[2].(int) != 1 {
    22  		t.Errorf("Got %v expected %v", actualValue, "[3,2,1]")
    23  	}
    24  	if actualValue := stack.Empty(); actualValue != false {
    25  		t.Errorf("Got %v expected %v", actualValue, false)
    26  	}
    27  	if actualValue := stack.Size(); actualValue != 3 {
    28  		t.Errorf("Got %v expected %v", actualValue, 3)
    29  	}
    30  	if actualValue, ok := stack.Peek(); actualValue != 3 || !ok {
    31  		t.Errorf("Got %v expected %v", actualValue, 3)
    32  	}
    33  }
    34  
    35  func TestStackPeek(t *testing.T) {
    36  	stack := New()
    37  	if actualValue, ok := stack.Peek(); actualValue != nil || ok {
    38  		t.Errorf("Got %v expected %v", actualValue, nil)
    39  	}
    40  	stack.Push(1)
    41  	stack.Push(2)
    42  	stack.Push(3)
    43  	if actualValue, ok := stack.Peek(); actualValue != 3 || !ok {
    44  		t.Errorf("Got %v expected %v", actualValue, 3)
    45  	}
    46  }
    47  
    48  func TestStackPop(t *testing.T) {
    49  	stack := New()
    50  	stack.Push(1)
    51  	stack.Push(2)
    52  	stack.Push(3)
    53  	stack.Pop()
    54  	if actualValue, ok := stack.Peek(); actualValue != 2 || !ok {
    55  		t.Errorf("Got %v expected %v", actualValue, 2)
    56  	}
    57  	if actualValue, ok := stack.Pop(); actualValue != 2 || !ok {
    58  		t.Errorf("Got %v expected %v", actualValue, 2)
    59  	}
    60  	if actualValue, ok := stack.Pop(); actualValue != 1 || !ok {
    61  		t.Errorf("Got %v expected %v", actualValue, 1)
    62  	}
    63  	if actualValue, ok := stack.Pop(); actualValue != nil || ok {
    64  		t.Errorf("Got %v expected %v", actualValue, nil)
    65  	}
    66  	if actualValue := stack.Empty(); actualValue != true {
    67  		t.Errorf("Got %v expected %v", actualValue, true)
    68  	}
    69  	if actualValue := stack.Values(); len(actualValue) != 0 {
    70  		t.Errorf("Got %v expected %v", actualValue, "[]")
    71  	}
    72  }
    73  
    74  func TestStackIterator(t *testing.T) {
    75  	stack := New()
    76  	stack.Push("a")
    77  	stack.Push("b")
    78  	stack.Push("c")
    79  
    80  	// Iterator
    81  	it := stack.Iterator()
    82  	count := 0
    83  	for it.Next() {
    84  		count++
    85  		index := it.Index()
    86  		value := it.Value()
    87  		switch index {
    88  		case 0:
    89  			if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
    90  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
    91  			}
    92  		case 1:
    93  			if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
    94  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
    95  			}
    96  		case 2:
    97  			if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
    98  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
    99  			}
   100  		default:
   101  			t.Errorf("Too many")
   102  		}
   103  		if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
   104  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   105  		}
   106  	}
   107  	if actualValue, expectedValue := count, 3; actualValue != expectedValue {
   108  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   109  	}
   110  
   111  	stack.Clear()
   112  	it = stack.Iterator()
   113  	for it.Next() {
   114  		t.Errorf("Shouldn't iterate on empty stack")
   115  	}
   116  }
   117  
   118  func TestStackIteratorBegin(t *testing.T) {
   119  	stack := New()
   120  	it := stack.Iterator()
   121  	it.Begin()
   122  	stack.Push("a")
   123  	stack.Push("b")
   124  	stack.Push("c")
   125  	for it.Next() {
   126  	}
   127  	it.Begin()
   128  	it.Next()
   129  	if index, value := it.Index(), it.Value(); index != 0 || value != "c" {
   130  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c")
   131  	}
   132  }
   133  
   134  func TestStackIteratorFirst(t *testing.T) {
   135  	stack := New()
   136  	it := stack.Iterator()
   137  	if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
   138  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   139  	}
   140  	stack.Push("a")
   141  	stack.Push("b")
   142  	stack.Push("c")
   143  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   144  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   145  	}
   146  	if index, value := it.Index(), it.Value(); index != 0 || value != "c" {
   147  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c")
   148  	}
   149  }
   150  
   151  func TestStackSerialization(t *testing.T) {
   152  	stack := New()
   153  	stack.Push("a")
   154  	stack.Push("b")
   155  	stack.Push("c")
   156  
   157  	var err error
   158  	assert := func() {
   159  		if actualValue, expectedValue := fmt.Sprintf("%s%s%s", stack.Values()...), "cba"; actualValue != expectedValue {
   160  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   161  		}
   162  		if actualValue, expectedValue := stack.Size(), 3; actualValue != expectedValue {
   163  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   164  		}
   165  		if err != nil {
   166  			t.Errorf("Got error %v", err)
   167  		}
   168  	}
   169  
   170  	assert()
   171  
   172  	json, err := stack.ToJSON()
   173  	assert()
   174  
   175  	err = stack.FromJSON(json)
   176  	assert()
   177  }
   178  
   179  func benchmarkPush(b *testing.B, stack *Stack, size int) {
   180  	for i := 0; i < b.N; i++ {
   181  		for n := 0; n < size; n++ {
   182  			stack.Push(n)
   183  		}
   184  	}
   185  }
   186  
   187  func benchmarkPop(b *testing.B, stack *Stack, size int) {
   188  	for i := 0; i < b.N; i++ {
   189  		for n := 0; n < size; n++ {
   190  			stack.Pop()
   191  		}
   192  	}
   193  }
   194  
   195  func BenchmarkLinkedListStackPop100(b *testing.B) {
   196  	b.StopTimer()
   197  	size := 100
   198  	stack := New()
   199  	for n := 0; n < size; n++ {
   200  		stack.Push(n)
   201  	}
   202  	b.StartTimer()
   203  	benchmarkPop(b, stack, size)
   204  }
   205  
   206  func BenchmarkLinkedListStackPop1000(b *testing.B) {
   207  	b.StopTimer()
   208  	size := 1000
   209  	stack := New()
   210  	for n := 0; n < size; n++ {
   211  		stack.Push(n)
   212  	}
   213  	b.StartTimer()
   214  	benchmarkPop(b, stack, size)
   215  }
   216  
   217  func BenchmarkLinkedListStackPop10000(b *testing.B) {
   218  	b.StopTimer()
   219  	size := 10000
   220  	stack := New()
   221  	for n := 0; n < size; n++ {
   222  		stack.Push(n)
   223  	}
   224  	b.StartTimer()
   225  	benchmarkPop(b, stack, size)
   226  }
   227  
   228  func BenchmarkLinkedListStackPop100000(b *testing.B) {
   229  	b.StopTimer()
   230  	size := 100000
   231  	stack := New()
   232  	for n := 0; n < size; n++ {
   233  		stack.Push(n)
   234  	}
   235  	b.StartTimer()
   236  	benchmarkPop(b, stack, size)
   237  }
   238  
   239  func BenchmarkLinkedListStackPush100(b *testing.B) {
   240  	b.StopTimer()
   241  	size := 100
   242  	stack := New()
   243  	b.StartTimer()
   244  	benchmarkPush(b, stack, size)
   245  }
   246  
   247  func BenchmarkLinkedListStackPush1000(b *testing.B) {
   248  	b.StopTimer()
   249  	size := 1000
   250  	stack := New()
   251  	for n := 0; n < size; n++ {
   252  		stack.Push(n)
   253  	}
   254  	b.StartTimer()
   255  	benchmarkPush(b, stack, size)
   256  }
   257  
   258  func BenchmarkLinkedListStackPush10000(b *testing.B) {
   259  	b.StopTimer()
   260  	size := 10000
   261  	stack := New()
   262  	for n := 0; n < size; n++ {
   263  		stack.Push(n)
   264  	}
   265  	b.StartTimer()
   266  	benchmarkPush(b, stack, size)
   267  }
   268  
   269  func BenchmarkLinkedListStackPush100000(b *testing.B) {
   270  	b.StopTimer()
   271  	size := 100000
   272  	stack := New()
   273  	for n := 0; n < size; n++ {
   274  		stack.Push(n)
   275  	}
   276  	b.StartTimer()
   277  	benchmarkPush(b, stack, size)
   278  }