github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/internal/deque_test.go (about) 1 package internal 2 3 import "testing" 4 5 func TestDeque(t *testing.T) { 6 t.Run("pop", func(t *testing.T) { 7 var dq Deque[int] 8 dq.Push(1) 9 dq.Push(2) 10 11 if dq.Pop() != 2 { 12 t.Error("Didn't pop 2 first") 13 } 14 15 if dq.Pop() != 1 { 16 t.Error("Didn't pop 1 second") 17 } 18 19 if dq.Pop() != 0 { 20 t.Error("Didn't pop zero") 21 } 22 }) 23 24 t.Run("shift", func(t *testing.T) { 25 var td Deque[int] 26 td.Push(1) 27 td.Push(2) 28 29 if td.Shift() != 1 { 30 t.Error("Didn't shift 1 first") 31 } 32 33 if td.Shift() != 2 { 34 t.Error("Didn't shift b second") 35 } 36 37 if td.Shift() != 0 { 38 t.Error("Didn't shift zero") 39 } 40 }) 41 42 t.Run("push", func(t *testing.T) { 43 var td Deque[int] 44 td.Push(1) 45 td.Push(2) 46 td.Shift() 47 48 for i := 1; i <= 12; i++ { 49 td.Push(i) 50 } 51 52 if td.Shift() != 2 { 53 t.Error("Didn't shift 2 first") 54 } 55 for i := 1; i <= 12; i++ { 56 if v := td.Shift(); v != i { 57 t.Fatalf("Shifted %d at pos %d", v, i) 58 } 59 } 60 }) 61 62 t.Run("grow", func(t *testing.T) { 63 var td Deque[int] 64 td.Push(1) 65 td.Push(2) 66 td.Push(3) 67 td.Shift() 68 69 td.Grow(7) 70 if len(td.elems) < 9 { 71 t.Fatal("Expected at least 9 elements, got", len(td.elems)) 72 } 73 74 if cap(td.elems)&(cap(td.elems)-1) != 0 { 75 t.Fatalf("Capacity %d is not a power of two", cap(td.elems)) 76 } 77 78 if td.Shift() != 2 || td.Shift() != 3 { 79 t.Fatal("Elements don't match after grow") 80 } 81 }) 82 }