github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/common/prque/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  	"math/rand"
    13  	"testing"
    14  )
    15  
    16  func TestPrque(t *testing.T) {
    17  	// Generate a batch of random data and a specific priority order
    18  	size := 16 * blockSize
    19  	prio := rand.Perm(size)
    20  	data := make([]int, size)
    21  	for i := 0; i < size; i++ {
    22  		data[i] = rand.Int()
    23  	}
    24  	queue := New(nil)
    25  	for rep := 0; rep < 2; rep++ {
    26  		// Fill a priority queue with the above data
    27  		for i := 0; i < size; i++ {
    28  			queue.Push(data[i], int64(prio[i]))
    29  			if queue.Size() != i+1 {
    30  				t.Errorf("queue size mismatch: have %v, want %v.", queue.Size(), i+1)
    31  			}
    32  		}
    33  		// Create a map the values to the priorities for easier verification
    34  		dict := make(map[int64]int)
    35  		for i := 0; i < size; i++ {
    36  			dict[int64(prio[i])] = data[i]
    37  		}
    38  		// Pop out the elements in priority order and verify them
    39  		prevPrio := int64(size + 1)
    40  		for !queue.Empty() {
    41  			val, prio := queue.Pop()
    42  			if prio > prevPrio {
    43  				t.Errorf("invalid priority order: %v after %v.", prio, prevPrio)
    44  			}
    45  			prevPrio = prio
    46  			if val != dict[prio] {
    47  				t.Errorf("push/pop mismatch: have %v, want %v.", val, dict[prio])
    48  			}
    49  			delete(dict, prio)
    50  		}
    51  	}
    52  }
    53  
    54  func TestReset(t *testing.T) {
    55  	// Generate a batch of random data and a specific priority order
    56  	size := 16 * blockSize
    57  	prio := rand.Perm(size)
    58  	data := make([]int, size)
    59  	for i := 0; i < size; i++ {
    60  		data[i] = rand.Int()
    61  	}
    62  	queue := New(nil)
    63  	for rep := 0; rep < 2; rep++ {
    64  		// Fill a priority queue with the above data
    65  		for i := 0; i < size; i++ {
    66  			queue.Push(data[i], int64(prio[i]))
    67  			if queue.Size() != i+1 {
    68  				t.Errorf("queue size mismatch: have %v, want %v.", queue.Size(), i+1)
    69  			}
    70  		}
    71  		// Create a map the values to the priorities for easier verification
    72  		dict := make(map[int64]int)
    73  		for i := 0; i < size; i++ {
    74  			dict[int64(prio[i])] = data[i]
    75  		}
    76  		// Pop out half the elements in priority order and verify them
    77  		prevPrio := int64(size + 1)
    78  		for i := 0; i < size/2; i++ {
    79  			val, prio := queue.Pop()
    80  			if prio > prevPrio {
    81  				t.Errorf("invalid priority order: %v after %v.", prio, prevPrio)
    82  			}
    83  			prevPrio = prio
    84  			if val != dict[prio] {
    85  				t.Errorf("push/pop mismatch: have %v, want %v.", val, dict[prio])
    86  			}
    87  			delete(dict, prio)
    88  		}
    89  		// Reset and ensure it's empty
    90  		queue.Reset()
    91  		if !queue.Empty() {
    92  			t.Errorf("priority queue not empty after reset: %v", queue)
    93  		}
    94  	}
    95  }
    96  
    97  func BenchmarkPush(b *testing.B) {
    98  	// Create some initial data
    99  	data := make([]int, b.N)
   100  	prio := make([]int64, b.N)
   101  	for i := 0; i < len(data); i++ {
   102  		data[i] = rand.Int()
   103  		prio[i] = rand.Int63()
   104  	}
   105  	// Execute the benchmark
   106  	b.ResetTimer()
   107  	queue := New(nil)
   108  	for i := 0; i < len(data); i++ {
   109  		queue.Push(data[i], prio[i])
   110  	}
   111  }
   112  
   113  func BenchmarkPop(b *testing.B) {
   114  	// Create some initial data
   115  	data := make([]int, b.N)
   116  	prio := make([]int64, b.N)
   117  	for i := 0; i < len(data); i++ {
   118  		data[i] = rand.Int()
   119  		prio[i] = rand.Int63()
   120  	}
   121  	queue := New(nil)
   122  	for i := 0; i < len(data); i++ {
   123  		queue.Push(data[i], prio[i])
   124  	}
   125  	// Execute the benchmark
   126  	b.ResetTimer()
   127  	for !queue.Empty() {
   128  		queue.Pop()
   129  	}
   130  }