github.com/ngicks/gokugen@v0.0.5/heap/heap_test.go (about)

     1  package heap_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/ngicks/gokugen/heap"
     9  )
    10  
    11  func TestMinHeap(t *testing.T) {
    12  	// Seeing basic delegation.
    13  	// If these codes work, it should work for any type.
    14  	t.Run("number heap", func(t *testing.T) {
    15  		h := heap.NewNumber[int]()
    16  		ans := []int{3, 4, 4, 5, 6}
    17  		h.Push(5)
    18  		h.Push(4)
    19  		h.Push(6)
    20  		h.Push(3)
    21  		h.Push(4)
    22  
    23  		for _, i := range ans {
    24  			popped := h.Pop()
    25  			if popped != i {
    26  				t.Errorf("pop = %v expected %v", popped, i)
    27  			}
    28  		}
    29  		if h.Len() != 0 {
    30  			t.Errorf("expect empty but size = %v", h.Len())
    31  		}
    32  	})
    33  
    34  	t.Run("struct heap", func(t *testing.T) {
    35  		type testStruct struct {
    36  			t time.Time
    37  		}
    38  		less := func(i, j *testStruct) bool {
    39  			return i.t.Before(j.t)
    40  		}
    41  
    42  		h := heap.NewHeap(less)
    43  		ans := []*testStruct{
    44  			{t: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)},
    45  			{t: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC)},
    46  			{t: time.Date(2021, 3, 1, 0, 0, 0, 0, time.UTC)},
    47  			{t: time.Date(2021, 4, 1, 0, 0, 0, 0, time.UTC)},
    48  			{t: time.Date(2021, 5, 1, 0, 0, 0, 0, time.UTC)},
    49  		}
    50  		h.Push(ans[2])
    51  		h.Push(ans[1])
    52  		h.Push(ans[3])
    53  		h.Push(ans[0])
    54  		h.Push(ans[4])
    55  
    56  		for _, i := range ans {
    57  			popped := h.Pop()
    58  			if popped.t != i.t {
    59  				t.Errorf("pop = %v expected %v", popped.t, i.t)
    60  			}
    61  		}
    62  		if h.Len() != 0 {
    63  			t.Errorf("expect empty but size = %v", h.Len())
    64  		}
    65  	})
    66  
    67  	t.Run("Exclude", func(t *testing.T) {
    68  		h := heap.NewNumber[int]()
    69  		h.Push(7)
    70  		h.Push(4)
    71  		h.Push(1)
    72  		h.Push(6)
    73  		h.Push(5)
    74  		h.Push(3)
    75  		h.Push(2)
    76  
    77  		removed := h.Exclude(func(ent int) bool { return ent%2 == 0 }, -1, 100)
    78  
    79  		fmt.Println(removed)
    80  
    81  		for i := 1; i <= 7; i = i + 2 {
    82  			popped := h.Pop()
    83  			if popped != i {
    84  				t.Errorf("pop = %v expected %v", popped, i)
    85  			}
    86  		}
    87  		if h.Len() != 0 {
    88  			t.Errorf("expect empty but size = %v", h.Len())
    89  		}
    90  
    91  		h.Push(1)
    92  		h.Push(3)
    93  		h.Push(5)
    94  		h.Push(7)
    95  		h.Push(9)
    96  		h.Push(11)
    97  		h.Push(13)
    98  
    99  		removed = h.Exclude(func(ent int) bool { return ent%2 != 0 }, 0, 3)
   100  
   101  		if len(removed) != 3 {
   102  			t.Fatalf("removed len must be %d, but is %d: %v", 3, len(removed), removed)
   103  		}
   104  		for h.Len() != 0 {
   105  			h.Pop()
   106  		}
   107  
   108  		h.Push(1)
   109  		h.Push(3)
   110  		h.Push(5)
   111  		h.Push(7)
   112  		h.Push(9)
   113  		h.Push(11)
   114  		h.Push(13)
   115  
   116  		removed = h.Exclude(func(ent int) bool { return ent%2 != 0 }, 3, 6)
   117  
   118  		if len(removed) != 3 {
   119  			t.Fatalf("removed len must be %d, but is %d: %v", 3, len(removed), removed)
   120  		}
   121  	})
   122  }