github.com/bj-wangjia/priority_queue@v0.0.1/priority_queue_test.go (about)

     1  package priority_queue
     2  
     3  import (
     4  	"math/rand"
     5  	"sort"
     6  	"testing"
     7  )
     8  
     9  type Int int
    10  
    11  func (this Int) Less(other interface{}) bool {
    12  	return this < other.(Int)
    13  }
    14  
    15  type IntSorter []Int
    16  
    17  func (s *IntSorter) Len() int {
    18  	return len(*s)
    19  }
    20  
    21  func (s *IntSorter) Less(i, j int) bool {
    22  	return (*s)[i] < (*s)[j]
    23  }
    24  
    25  func (s *IntSorter) Swap(i, j int) {
    26  	(*s)[i], (*s)[j] = (*s)[j], (*s)[i]
    27  }
    28  
    29  func TestInt(t *testing.T) {
    30  	q := New()
    31  
    32  	if q.Len() != 0 {
    33  		t.Fatal()
    34  	}
    35  
    36  	q.Push(Int(-1))
    37  	for i := 0; i < 998; i++ {
    38  		q.Push(Int(rand.Intn(100)))
    39  	}
    40  	q.Push(Int(5201314))
    41  
    42  	s := new(IntSorter)
    43  	n := 1000
    44  	for q.Len() > 0 {
    45  		if q.Len() != n {
    46  			t.Fatal()
    47  		}
    48  		n--
    49  
    50  		x := q.Top()
    51  		y := q.Pop()
    52  		if x != y || x.(Int) != y.(Int) {
    53  			t.Fatal()
    54  		}
    55  
    56  		*s = append(*s, x.(Int))
    57  	}
    58  
    59  	if (*s)[0] != -1 || (*s)[999] != 5201314 {
    60  		t.Fatal()
    61  	}
    62  
    63  	if !sort.IsSorted(s) {
    64  		t.Fatal()
    65  	}
    66  
    67  	q = New()
    68  
    69  	if q.Len() != 0 {
    70  		t.Fatal()
    71  	}
    72  }
    73  
    74  func TestFixAndRemove(t *testing.T) {
    75  	q := New()
    76  	q.Push(Int(1))
    77  	q.Push(Int(3))
    78  	q.Push(Int(2))
    79  	q.Push(Int(4))
    80  
    81  	if (*q.s)[0].(Int) != 1 || (*q.s)[1].(Int) != 3 || (*q.s)[2].(Int) != 2 || (*q.s)[3].(Int) != 4 {
    82  		t.Fatal()
    83  	}
    84  
    85  	q.Fix(Int(5), 1)
    86  
    87  	if (*q.s)[0].(Int) != 1 || (*q.s)[1].(Int) != 4 || (*q.s)[2].(Int) != 2 || (*q.s)[3].(Int) != 5 {
    88  		t.Fatal()
    89  	}
    90  
    91  	a, b, c, d := q.Pop(), q.Pop(), q.Pop(), q.Pop()
    92  	if a.(Int) != 1 || b.(Int) != 2 || c.(Int) != 4 || d.(Int) != 5 {
    93  		t.Fatal()
    94  	}
    95  
    96  	if q.Len() != 0 {
    97  		t.Fatal()
    98  	}
    99  
   100  	q.Push(Int(8))
   101  	q.Push(Int(6))
   102  	q.Push(Int(7))
   103  	q.Push(Int(9))
   104  
   105  	// println((*q.s)[0].(Int), (*q.s)[1].(Int), (*q.s)[2].(Int), (*q.s)[3].(Int))
   106  
   107  	if (*q.s)[0].(Int) != 6 || (*q.s)[1].(Int) != 8 || (*q.s)[2].(Int) != 7 || (*q.s)[3].(Int) != 9 {
   108  		t.Fatal()
   109  	}
   110  
   111  	if q.Top().(Int) != 6 {
   112  		t.Fatal()
   113  	}
   114  
   115  	q.Remove(0)
   116  
   117  	if q.Top().(Int) != 7 {
   118  		t.Fatal()
   119  	}
   120  
   121  	a, b, c = q.Pop(), q.Pop(), q.Pop()
   122  	if a.(Int) != 7 || b.(Int) != 8 || c.(Int) != 9 {
   123  		t.Fatal()
   124  	}
   125  }
   126  
   127  type Node struct {
   128  	priority int
   129  	value    int
   130  }
   131  
   132  func NewNode(p, v int) *Node {
   133  	return &Node{priority: p}
   134  }
   135  
   136  func (this *Node) Less(other interface{}) bool {
   137  	return this.priority < other.(*Node).priority
   138  }
   139  
   140  func TestStruct(t *testing.T) {
   141  	q := New()
   142  	for i := 0; i < 1000; i++ {
   143  		q.Push(NewNode(rand.Intn(100000), i))
   144  	}
   145  
   146  	n := 1000
   147  	for q.Len() > 0 {
   148  		if q.Len() != n {
   149  			t.Fatal()
   150  		}
   151  		n--
   152  
   153  		x := q.Top().(*Node)
   154  		y := q.Pop().(*Node)
   155  
   156  		if x.priority != y.priority || x.value != y.value {
   157  			t.Fatal()
   158  		}
   159  	}
   160  }
   161  
   162  func BenchmarkPush1(b *testing.B) {
   163  	b.StopTimer()
   164  	q := New()
   165  	b.StartTimer()
   166  	for i := 0; i < 100000; i++ {
   167  		q.Push(NewNode(rand.Intn(100), i))
   168  	}
   169  }
   170  
   171  func BenchmarkPush2(b *testing.B) {
   172  	b.StopTimer()
   173  	q := New()
   174  	b.StartTimer()
   175  	for i := 0; i < 500000; i++ {
   176  		q.Push(NewNode(rand.Intn(100), i))
   177  	}
   178  }
   179  
   180  func BenchmarkPush3(b *testing.B) {
   181  	b.StopTimer()
   182  	q := New()
   183  	b.StartTimer()
   184  	for i := 0; i < 1000000; i++ {
   185  		q.Push(NewNode(rand.Intn(100), i))
   186  	}
   187  }
   188  
   189  func BenchmarkPop1(b *testing.B) {
   190  	b.StopTimer()
   191  	q := New()
   192  	for i := 0; i < 100000; i++ {
   193  		q.Push(NewNode(rand.Intn(100), i))
   194  	}
   195  
   196  	b.StartTimer()
   197  	for q.Len() > 0 {
   198  		q.Pop()
   199  	}
   200  }
   201  
   202  func BenchmarkPop2(b *testing.B) {
   203  	b.StopTimer()
   204  	q := New()
   205  	for i := 0; i < 500000; i++ {
   206  		q.Push(NewNode(rand.Intn(100), i))
   207  	}
   208  
   209  	b.StartTimer()
   210  	for q.Len() > 0 {
   211  		q.Pop()
   212  	}
   213  }
   214  
   215  func BenchmarkPop3(b *testing.B) {
   216  	b.StopTimer()
   217  	q := New()
   218  	for i := 0; i < 1000000; i++ {
   219  		q.Push(NewNode(rand.Intn(100), i))
   220  	}
   221  
   222  	b.StartTimer()
   223  	for q.Len() > 0 {
   224  		q.Pop()
   225  	}
   226  }