github.com/cilium/ebpf@v0.10.0/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("linearise", func(t *testing.T) { 63 var td Deque[int] 64 td.Push(1) 65 td.Push(2) 66 67 all := td.linearise(0) 68 if len(all) != 2 { 69 t.Fatal("Expected 2 elements, got", len(all)) 70 } 71 72 if cap(all)&(cap(all)-1) != 0 { 73 t.Fatalf("Capacity %d is not a power of two", cap(all)) 74 } 75 76 if all[0] != 1 || all[1] != 2 { 77 t.Fatal("Elements don't match") 78 } 79 }) 80 }