github.com/chain5j/chain5j-pkg@v1.0.7/collection/queues/preque/prque_test.go (about)

     1  // CookieJar - A contestant's algorithm toolbox
     2  // Copyright (c) 2013 Peter Szilagyi. All rights reserved.
     3  //
     4  // CookieJar is dual licensed: use of this source code is governed by a BSD
     5  // license that can be found in the LICENSE file. Alternatively, the CookieJar
     6  // toolbox may be used in accordance with the terms and conditions contained
     7  // in a signed written agreement between you and the author(s).
     8  
     9  package prque
    10  
    11  import (
    12  	"fmt"
    13  	"math/rand"
    14  	"sort"
    15  	"testing"
    16  )
    17  
    18  func TestPrque(t *testing.T) {
    19  	// Generate a batch of random data and a specific priority order
    20  	size := 16 * blockSize
    21  	prio := rand.Perm(size)
    22  	data := make([]int, size)
    23  	for i := 0; i < size; i++ {
    24  		data[i] = rand.Int()
    25  	}
    26  	queue := New()
    27  	for rep := 0; rep < 2; rep++ {
    28  		// Fill a priority queue with the above data
    29  		for i := 0; i < size; i++ {
    30  			queue.Push(data[i], float32(prio[i]))
    31  			if queue.Size() != i+1 {
    32  				t.Errorf("queue size mismatch: have %v, want %v.", queue.Size(), i+1)
    33  			}
    34  		}
    35  		// Create a map the values to the priorities for easier verification
    36  		dict := make(map[float32]int)
    37  		for i := 0; i < size; i++ {
    38  			dict[float32(prio[i])] = data[i]
    39  		}
    40  		// Pop out the elements in priority order and verify them
    41  		prevPrio := float32(size + 1)
    42  		for !queue.Empty() {
    43  			val, prio := queue.Pop()
    44  			if prio > prevPrio {
    45  				t.Errorf("invalid priority order: %v after %v.", prio, prevPrio)
    46  			}
    47  			prevPrio = prio
    48  			if val != dict[prio] {
    49  				t.Errorf("push/pop mismatch: have %v, want %v.", val, dict[prio])
    50  			}
    51  			delete(dict, prio)
    52  		}
    53  	}
    54  }
    55  
    56  func TestReset(t *testing.T) {
    57  	// Generate a batch of random data and a specific priority order
    58  	size := 16 * blockSize
    59  	prio := rand.Perm(size)
    60  	data := make([]int, size)
    61  	for i := 0; i < size; i++ {
    62  		data[i] = rand.Int()
    63  	}
    64  	queue := New()
    65  	for rep := 0; rep < 2; rep++ {
    66  		// Fill a priority queue with the above data
    67  		for i := 0; i < size; i++ {
    68  			queue.Push(data[i], float32(prio[i]))
    69  			if queue.Size() != i+1 {
    70  				t.Errorf("queue size mismatch: have %v, want %v.", queue.Size(), i+1)
    71  			}
    72  		}
    73  		// Create a map the values to the priorities for easier verification
    74  		dict := make(map[float32]int)
    75  		for i := 0; i < size; i++ {
    76  			dict[float32(prio[i])] = data[i]
    77  		}
    78  		// Pop out half the elements in priority order and verify them
    79  		prevPrio := float32(size + 1)
    80  		for i := 0; i < size/2; i++ {
    81  			val, prio := queue.Pop()
    82  			if prio > prevPrio {
    83  				t.Errorf("invalid priority order: %v after %v.", prio, prevPrio)
    84  			}
    85  			prevPrio = prio
    86  			if val != dict[prio] {
    87  				t.Errorf("push/pop mismatch: have %v, want %v.", val, dict[prio])
    88  			}
    89  			delete(dict, prio)
    90  		}
    91  		// Reset and ensure it's empty
    92  		queue.Reset()
    93  		if !queue.Empty() {
    94  			t.Errorf("priority queue not empty after reset: %v", queue)
    95  		}
    96  	}
    97  }
    98  
    99  func BenchmarkPush(b *testing.B) {
   100  	// Create some initial data
   101  	data := make([]int, b.N)
   102  	prio := make([]float32, b.N)
   103  	for i := 0; i < len(data); i++ {
   104  		data[i] = rand.Int()
   105  		prio[i] = rand.Float32()
   106  	}
   107  	// Execute the benchmark
   108  	b.ResetTimer()
   109  	queue := New()
   110  	for i := 0; i < len(data); i++ {
   111  		queue.Push(data[i], prio[i])
   112  	}
   113  }
   114  
   115  func BenchmarkPop(b *testing.B) {
   116  	// Create some initial data
   117  	data := make([]int, b.N)
   118  	prio := make([]float32, b.N)
   119  	for i := 0; i < len(data); i++ {
   120  		data[i] = rand.Int()
   121  		prio[i] = rand.Float32()
   122  	}
   123  	queue := New()
   124  	for i := 0; i < len(data); i++ {
   125  		queue.Push(data[i], prio[i])
   126  	}
   127  	// Execute the benchmark
   128  	b.ResetTimer()
   129  	for !queue.Empty() {
   130  		queue.Pop()
   131  	}
   132  }
   133  
   134  func TestPeek(t *testing.T) {
   135  	prque := New()
   136  	prque.Push("1", 1)
   137  	prque.Push("3", 3)
   138  	prque.Push("5", 5)
   139  	prque.Push("2", 2)
   140  	prque.Push("4", 4)
   141  	sort.Sort(prque.cont)
   142  	front, f := prque.PeekFront()
   143  	fmt.Println(front, f)
   144  
   145  }