v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/container2/ring2/ring2_test.go (about) 1 package ring2_test 2 3 import ( 4 "testing" 5 6 "v8.run/go/exp/container2/ring2" 7 ) 8 9 func TestRing2(t *testing.T) { 10 r := ring2.NewRing2[uint64](3) 11 if r.Len() != 0 { 12 t.Errorf("expected len 0, got %d", r.Len()) 13 } 14 15 if r.Cap() != 3 { 16 t.Errorf("expected cap 3, got %d", r.Cap()) 17 } 18 19 if r.Free() != 3 { 20 t.Errorf("expected free 3, got %d", r.Free()) 21 } 22 23 if !r.Write(1) { 24 t.Errorf("expected write to succeed") 25 } 26 27 if r.Len() != 1 { 28 t.Errorf("expected len 1, got %d", r.Len()) 29 } 30 31 if r.Free() != 2 { 32 t.Errorf("expected free 2, got %d", r.Free()) 33 } 34 35 if !r.Write(2) { 36 t.Errorf("expected write to succeed") 37 } 38 39 if !r.Write(3) { 40 t.Errorf("expected write to succeed") 41 } 42 43 if r.Write(4) { 44 t.Errorf("expected write to fail") 45 } 46 47 data, ok := r.Read() 48 if !ok { 49 t.Errorf("expected read to succeed") 50 } 51 52 if data != 1 { 53 t.Errorf("expected data 1, got %d", data) 54 } 55 56 if r.Len() != 2 { 57 t.Errorf("expected len 2, got %d", r.Len()) 58 } 59 60 if r.Free() != 1 { 61 t.Errorf("expected free 1, got %d", r.Free()) 62 } 63 64 if !r.Write(4) { 65 t.Errorf("expected write to succeed") 66 } 67 68 if r.Write(5) { 69 t.Errorf("expected write to fail") 70 } 71 72 data, ok = r.Read() 73 if !ok { 74 t.Errorf("expected read to succeed") 75 } 76 77 if data != 2 { 78 t.Errorf("expected data 2, got %d", data) 79 } 80 81 data, ok = r.Read() 82 if !ok { 83 t.Errorf("expected read to succeed") 84 } 85 86 if data != 3 { 87 t.Errorf("expected data 3, got %d", data) 88 } 89 90 data, ok = r.Read() 91 if !ok { 92 t.Errorf("expected read to succeed") 93 } 94 95 if data != 4 { 96 t.Errorf("expected data 4, got %d", data) 97 } 98 99 if r.Len() != 0 { 100 t.Errorf("expected len 0, got %d", r.Len()) 101 } 102 103 if r.Free() != 3 { 104 t.Errorf("expected free 3, got %d", r.Free()) 105 } 106 107 _, ok = r.Read() 108 if ok { 109 t.Errorf("expected read to fail") 110 } 111 } 112 113 func TestRing2At(t *testing.T) { 114 r := ring2.NewRing2[uint64](3) 115 r.Write(1) 116 r.Write(2) 117 r.Write(3) 118 r.Read() 119 r.Read() 120 r.Write(4) 121 r.Write(5) 122 123 if r.At(0) != 3 { 124 t.Errorf("expected data 3, got %d", r.At(0)) 125 } 126 127 if r.At(1) != 4 { 128 t.Errorf("expected data 4, got %d", r.At(1)) 129 } 130 131 if r.At(2) != 5 { 132 t.Errorf("expected data 5, got %d", r.At(2)) 133 } 134 } 135 136 func TestRing2Drop(t *testing.T) { 137 r := ring2.NewRing2[uint64](3) 138 r.Write(1) 139 r.Write(2) 140 r.Write(3) 141 r.Read() 142 r.Read() 143 r.Write(4) 144 r.Write(5) 145 r.Drop(2) 146 147 if r.Len() != 1 { 148 t.Errorf("expected len 1, got %d", r.Len()) 149 } 150 151 if r.Free() != 2 { 152 t.Errorf("expected free 2, got %d", r.Free()) 153 } 154 155 if r.At(0) != 5 { 156 t.Errorf("expected data 5, got %d", r.At(0)) 157 } 158 159 r.Reset() 160 161 if r.Len() != 0 { 162 t.Errorf("expected len 0, got %d", r.Len()) 163 } 164 165 if r.Free() != 3 { 166 t.Errorf("expected free 3, got %d", r.Free()) 167 } 168 169 r.Write(100) 170 r.Write(200) 171 r.Write(300) 172 r.Drop(1) 173 174 if r.Len() != 2 { 175 t.Errorf("expected len 2, got %d", r.Len()) 176 } 177 178 if r.Free() != 1 { 179 t.Errorf("expected free 1, got %d", r.Free()) 180 } 181 182 if r.At(0) != 200 { 183 t.Errorf("expected data 200, got %d", r.At(0)) 184 } 185 186 if r.At(1) != 300 { 187 t.Errorf("expected data 300, got %d", r.At(1)) 188 } 189 }