How to use Len method of got Package

Best Got code snippet using got.Len

immutable_test.go

Source:immutable_test.go Github

copy

Full Screen

...8func TestImmutableEmpty(t *testing.T) {9 t.Parallel()10 // Ensure the treap length is the expected value.11 testTreap := NewImmutable()12 if gotLen := testTreap.Len(); gotLen != 0 {13 t.Fatalf("Len: unexpected length - got %d, want %d", gotLen, 0)14 }15 // Ensure the reported size is 0.16 if gotSize := testTreap.Size(); gotSize != 0 {17 t.Fatalf("Size: unexpected byte size - got %d, want 0",18 gotSize,19 )20 }21 // Ensure there are no errors with requesting keys from an empty treap.22 key := serializeUint32(0)23 if gotVal := testTreap.Has(key); gotVal {24 t.Fatalf("Has: unexpected result - got %v, want false", gotVal)25 }26 if gotVal := testTreap.Get(key); gotVal != nil {27 t.Fatalf("Get: unexpected result - got %x, want nil", gotVal)28 }29 // Ensure there are no panics when deleting keys from an empty treap.30 testTreap.Delete(key)31 // Ensure the number of keys iterated by ForEach on an empty treap is32 // zero.33 var numIterated int34 testTreap.ForEach(func(k, v []byte) bool {35 numIterated++36 return true37 },38 )39 if numIterated != 0 {40 t.Fatalf("ForEach: unexpected iterate count - got %d, want 0",41 numIterated,42 )43 }44}45// TestImmutableSequential ensures that putting keys into an immutable treap in sequential order works as expected.46func TestImmutableSequential(t *testing.T) {47 t.Parallel()48 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.49 expectedSize := uint64(0)50 numItems := 100051 testTreap := NewImmutable()52 for i := 0; i < numItems; i++ {53 key := serializeUint32(uint32(i))54 testTreap = testTreap.Put(key, key)55 // Ensure the treap length is the expected value.56 if gotLen := testTreap.Len(); gotLen != i+1 {57 t.Fatalf("Len #%d: unexpected length - got %d, want %d",58 i, gotLen, i+1,59 )60 }61 // Ensure the treap has the key.62 if !testTreap.Has(key) {63 t.Fatalf("Has #%d: key %q is not in treap", i, key)64 }65 // Get the key from the treap and ensure it is the expected value.66 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {67 t.Fatalf("Get #%d: unexpected value - got %x, want %x",68 i, gotVal, key,69 )70 }71 // Ensure the expected size is reported.72 expectedSize += nodeFieldsSize + 873 if gotSize := testTreap.Size(); gotSize != expectedSize {74 t.Fatalf("Size #%d: unexpected byte size - got %d, "+75 "want %d", i, gotSize, expectedSize,76 )77 }78 }79 // Ensure the all keys are iterated by ForEach in order.80 var numIterated int81 testTreap.ForEach(func(k, v []byte) bool {82 wantKey := serializeUint32(uint32(numIterated))83 // Ensure the key is as expected.84 if !bytes.Equal(k, wantKey) {85 t.Fatalf("ForEach #%d: unexpected key - got %x, want %x",86 numIterated, k, wantKey,87 )88 }89 // Ensure the value is as expected.90 if !bytes.Equal(v, wantKey) {91 t.Fatalf("ForEach #%d: unexpected value - got %x, want %x",92 numIterated, v, wantKey,93 )94 }95 numIterated++96 return true97 },98 )99 // Ensure all items were iterated.100 if numIterated != numItems {101 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",102 numIterated, numItems,103 )104 }105 // Delete the keys one-by-one while checking several of the treap functions work as expected.106 for i := 0; i < numItems; i++ {107 key := serializeUint32(uint32(i))108 testTreap = testTreap.Delete(key)109 // Ensure the treap length is the expected value.110 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {111 t.Fatalf("Len #%d: unexpected length - got %d, want %d",112 i, gotLen, numItems-i-1,113 )114 }115 // Ensure the treap no longer has the key.116 if testTreap.Has(key) {117 t.Fatalf("Has #%d: key %q is in treap", i, key)118 }119 // Get the key that no longer exists from the treap and ensure it is nil.120 if gotVal := testTreap.Get(key); gotVal != nil {121 t.Fatalf("Get #%d: unexpected value - got %x, want nil",122 i, gotVal,123 )124 }125 // Ensure the expected size is reported.126 expectedSize -= nodeFieldsSize + 8127 if gotSize := testTreap.Size(); gotSize != expectedSize {128 t.Fatalf("Size #%d: unexpected byte size - got %d, "+129 "want %d", i, gotSize, expectedSize,130 )131 }132 }133}134// TestImmutableReverseSequential ensures that putting keys into an immutable treap in reverse sequential order works as135// expected.136func TestImmutableReverseSequential(t *testing.T) {137 t.Parallel()138 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.139 expectedSize := uint64(0)140 numItems := 1000141 testTreap := NewImmutable()142 for i := 0; i < numItems; i++ {143 key := serializeUint32(uint32(numItems - i - 1))144 testTreap = testTreap.Put(key, key)145 // Ensure the treap length is the expected value.146 if gotLen := testTreap.Len(); gotLen != i+1 {147 t.Fatalf("Len #%d: unexpected length - got %d, want %d",148 i, gotLen, i+1,149 )150 }151 // Ensure the treap has the key.152 if !testTreap.Has(key) {153 t.Fatalf("Has #%d: key %q is not in treap", i, key)154 }155 // Get the key from the treap and ensure it is the expected156 // value.157 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {158 t.Fatalf("Get #%d: unexpected value - got %x, want %x",159 i, gotVal, key,160 )161 }162 // Ensure the expected size is reported.163 expectedSize += nodeFieldsSize + 8164 if gotSize := testTreap.Size(); gotSize != expectedSize {165 t.Fatalf("Size #%d: unexpected byte size - got %d, "+166 "want %d", i, gotSize, expectedSize,167 )168 }169 }170 // Ensure the all keys are iterated by ForEach in order.171 var numIterated int172 testTreap.ForEach(func(k, v []byte) bool {173 wantKey := serializeUint32(uint32(numIterated))174 // Ensure the key is as expected.175 if !bytes.Equal(k, wantKey) {176 t.Fatalf("ForEach #%d: unexpected key - got %x, want %x",177 numIterated, k, wantKey,178 )179 }180 // Ensure the value is as expected.181 if !bytes.Equal(v, wantKey) {182 t.Fatalf("ForEach #%d: unexpected value - got %x, want %x",183 numIterated, v, wantKey,184 )185 }186 numIterated++187 return true188 },189 )190 // Ensure all items were iterated.191 if numIterated != numItems {192 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",193 numIterated, numItems,194 )195 }196 // Delete the keys one-by-one while checking several of the treap functions work as expected.197 for i := 0; i < numItems; i++ {198 // Intentionally use the reverse order they were inserted here.199 key := serializeUint32(uint32(i))200 testTreap = testTreap.Delete(key)201 // Ensure the treap length is the expected value.202 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {203 t.Fatalf("Len #%d: unexpected length - got %d, want %d",204 i, gotLen, numItems-i-1,205 )206 }207 // Ensure the treap no longer has the key.208 if testTreap.Has(key) {209 t.Fatalf("Has #%d: key %q is in treap", i, key)210 }211 // Get the key that no longer exists from the treap and ensure212 // it is nil.213 if gotVal := testTreap.Get(key); gotVal != nil {214 t.Fatalf("Get #%d: unexpected value - got %x, want nil",215 i, gotVal,216 )217 }218 // Ensure the expected size is reported.219 expectedSize -= nodeFieldsSize + 8220 if gotSize := testTreap.Size(); gotSize != expectedSize {221 t.Fatalf("Size #%d: unexpected byte size - got %d, "+222 "want %d", i, gotSize, expectedSize,223 )224 }225 }226}227// TestImmutableUnordered ensures that putting keys into an immutable treap in no particular order works as expected.228func TestImmutableUnordered(t *testing.T) {229 t.Parallel()230 // Insert a bunch of out-of-order keys while checking several of the treap functions work as expected.231 expectedSize := uint64(0)232 numItems := 1000233 testTreap := NewImmutable()234 for i := 0; i < numItems; i++ {235 // Hash the serialized int to generate out-of-order keys.236 hash := sha256.Sum256(serializeUint32(uint32(i)))237 key := hash[:]238 testTreap = testTreap.Put(key, key)239 // Ensure the treap length is the expected value.240 if gotLen := testTreap.Len(); gotLen != i+1 {241 t.Fatalf("Len #%d: unexpected length - got %d, want %d",242 i, gotLen, i+1,243 )244 }245 // Ensure the treap has the key.246 if !testTreap.Has(key) {247 t.Fatalf("Has #%d: key %q is not in treap", i, key)248 }249 // Get the key from the treap and ensure it is the expected value.250 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {251 t.Fatalf("Get #%d: unexpected value - got %x, want %x",252 i, gotVal, key,253 )254 }255 // Ensure the expected size is reported.256 expectedSize += nodeFieldsSize + uint64(len(key)+len(key))257 if gotSize := testTreap.Size(); gotSize != expectedSize {258 t.Fatalf("Size #%d: unexpected byte size - got %d, "+259 "want %d", i, gotSize, expectedSize,260 )261 }262 }263 // Delete the keys one-by-one while checking several of the treap functions work as expected.264 for i := 0; i < numItems; i++ {265 // Hash the serialized int to generate out-of-order keys.266 hash := sha256.Sum256(serializeUint32(uint32(i)))267 key := hash[:]268 testTreap = testTreap.Delete(key)269 // Ensure the treap length is the expected value.270 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {271 t.Fatalf("Len #%d: unexpected length - got %d, want %d",272 i, gotLen, numItems-i-1,273 )274 }275 // Ensure the treap no longer has the key.276 if testTreap.Has(key) {277 t.Fatalf("Has #%d: key %q is in treap", i, key)278 }279 // Get the key that no longer exists from the treap and ensure it is nil.280 if gotVal := testTreap.Get(key); gotVal != nil {281 t.Fatalf("Get #%d: unexpected value - got %x, want nil",282 i, gotVal,283 )284 }285 // Ensure the expected size is reported.286 expectedSize -= nodeFieldsSize + 64287 if gotSize := testTreap.Size(); gotSize != expectedSize {288 t.Fatalf("Size #%d: unexpected byte size - got %d, "+289 "want %d", i, gotSize, expectedSize,290 )291 }292 }293}294// TestImmutableDuplicatePut ensures that putting a duplicate key into an immutable treap works as expected.295func TestImmutableDuplicatePut(t *testing.T) {296 t.Parallel()297 expectedVal := []byte("testval")298 expectedSize := uint64(0)299 numItems := 1000300 testTreap := NewImmutable()301 for i := 0; i < numItems; i++ {302 key := serializeUint32(uint32(i))303 testTreap = testTreap.Put(key, key)304 expectedSize += nodeFieldsSize + uint64(len(key)+len(key))305 // Put a duplicate key with the the expected final value.306 testTreap = testTreap.Put(key, expectedVal)307 // Ensure the key still exists and is the new value.308 if gotVal := testTreap.Has(key); !gotVal {309 t.Fatalf("Has: unexpected result - got %v, want true",310 gotVal,311 )312 }313 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, expectedVal) {314 t.Fatalf("Get: unexpected result - got %x, want %x",315 gotVal, expectedVal,316 )317 }318 // Ensure the expected size is reported.319 expectedSize -= uint64(len(key))320 expectedSize += uint64(len(expectedVal))321 if gotSize := testTreap.Size(); gotSize != expectedSize {322 t.Fatalf("Size: unexpected byte size - got %d, want %d",323 gotSize, expectedSize,324 )325 }326 }327}328// TestImmutableNilValue ensures that putting a nil value into an immutable treap results in a key being added with an329// empty byte slice.330func TestImmutableNilValue(t *testing.T) {331 t.Parallel()332 key := serializeUint32(0)333 // Put the key with a nil value.334 testTreap := NewImmutable()335 testTreap = testTreap.Put(key, nil)336 // Ensure the key exists and is an empty byte slice.337 if gotVal := testTreap.Has(key); !gotVal {338 t.Fatalf("Has: unexpected result - got %v, want true", gotVal)339 }340 if gotVal := testTreap.Get(key); gotVal == nil {341 t.Fatalf("Get: unexpected result - got nil, want empty slice")342 }343 if gotVal := testTreap.Get(key); len(gotVal) != 0 {344 t.Fatalf("Get: unexpected result - got %x, want empty slice",345 gotVal,346 )347 }348}349// TestImmutableForEachStopIterator ensures that returning false from the ForEach callback on an immutable treap stops350// iteration early.351func TestImmutableForEachStopIterator(t *testing.T) {352 t.Parallel()353 // Insert a few keys.354 numItems := 10355 testTreap := NewImmutable()356 for i := 0; i < numItems; i++ {357 key := serializeUint32(uint32(i))358 testTreap = testTreap.Put(key, key)359 }360 // Ensure ForEach exits early on false return by caller.361 var numIterated int362 testTreap.ForEach(func(k, v []byte) bool {363 numIterated++364 return numIterated != numItems/2365 },366 )367 if numIterated != numItems/2 {368 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",369 numIterated, numItems/2,370 )371 }372}373// TestImmutableSnapshot ensures that immutable treaps are actually immutable by keeping a reference to the previous374// treap, performing a mutation, and then ensuring the referenced treap does not have the mutation applied.375func TestImmutableSnapshot(t *testing.T) {376 t.Parallel()377 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.378 expectedSize := uint64(0)379 numItems := 1000380 testTreap := NewImmutable()381 for i := 0; i < numItems; i++ {382 treapSnap := testTreap383 key := serializeUint32(uint32(i))384 testTreap = testTreap.Put(key, key)385 // Ensure the length of the treap snapshot is the expected386 // value.387 if gotLen := treapSnap.Len(); gotLen != i {388 t.Fatalf("Len #%d: unexpected length - got %d, want %d",389 i, gotLen, i,390 )391 }392 // Ensure the treap snapshot does not have the key.393 if treapSnap.Has(key) {394 t.Fatalf("Has #%d: key %q is in treap", i, key)395 }396 // Get the key that doesn't exist in the treap snapshot and397 // ensure it is nil.398 if gotVal := treapSnap.Get(key); gotVal != nil {399 t.Fatalf("Get #%d: unexpected value - got %x, want nil",400 i, gotVal,401 )402 }403 // Ensure the expected size is reported.404 if gotSize := treapSnap.Size(); gotSize != expectedSize {405 t.Fatalf("Size #%d: unexpected byte size - got %d, "+406 "want %d", i, gotSize, expectedSize,407 )408 }409 expectedSize += nodeFieldsSize + 8410 }411 // Delete the keys one-by-one while checking several of the treap functions work as expected.412 for i := 0; i < numItems; i++ {413 treapSnap := testTreap414 key := serializeUint32(uint32(i))415 testTreap = testTreap.Delete(key)416 // Ensure the length of the treap snapshot is the expected value.417 if gotLen := treapSnap.Len(); gotLen != numItems-i {418 t.Fatalf("Len #%d: unexpected length - got %d, want %d",419 i, gotLen, numItems-i,420 )421 }422 // Ensure the treap snapshot still has the key.423 if !treapSnap.Has(key) {424 t.Fatalf("Has #%d: key %q is not in treap", i, key)425 }426 // Get the key from the treap snapshot and ensure it is still the expected value.427 if gotVal := treapSnap.Get(key); !bytes.Equal(gotVal, key) {428 t.Fatalf("Get #%d: unexpected value - got %x, want %x",429 i, gotVal, key,430 )431 }432 // Ensure the expected size is reported.433 if gotSize := treapSnap.Size(); gotSize != expectedSize {...

Full Screen

Full Screen

mutable_test.go

Source:mutable_test.go Github

copy

Full Screen

...9func TestMutableEmpty(t *testing.T) {10 t.Parallel()11 // Ensure the treap length is the expected value.12 testTreap := NewMutable()13 if gotLen := testTreap.Len(); gotLen != 0 {14 t.Fatalf("Len: unexpected length - got %d, want %d", gotLen, 0)15 }16 // Ensure the reported size is 0.17 if gotSize := testTreap.Size(); gotSize != 0 {18 t.Fatalf("Size: unexpected byte size - got %d, want 0",19 gotSize,20 )21 }22 // Ensure there are no errors with requesting keys from an empty treap.23 key := serializeUint32(0)24 if gotVal := testTreap.Has(key); gotVal {25 t.Fatalf("Has: unexpected result - got %v, want false", gotVal)26 }27 if gotVal := testTreap.Get(key); gotVal != nil {28 t.Fatalf("Get: unexpected result - got %x, want nil", gotVal)29 }30 // Ensure there are no panics when deleting keys from an empty treap.31 testTreap.Delete(key)32 // Ensure the number of keys iterated by ForEach on an empty treap is33 // zero.34 var numIterated int35 testTreap.ForEach(func(k, v []byte) bool {36 numIterated++37 return true38 },39 )40 if numIterated != 0 {41 t.Fatalf("ForEach: unexpected iterate count - got %d, want 0",42 numIterated,43 )44 }45}46// TestMutableReset ensures that resetting an existing mutable treap works as expected.47func TestMutableReset(t *testing.T) {48 t.Parallel()49 // Insert a few keys.50 numItems := 1051 testTreap := NewMutable()52 for i := 0; i < numItems; i++ {53 key := serializeUint32(uint32(i))54 testTreap.Put(key, key)55 }56 // Reset it.57 testTreap.Reset()58 // Ensure the treap length is now 0.59 if gotLen := testTreap.Len(); gotLen != 0 {60 t.Fatalf("Len: unexpected length - got %d, want %d", gotLen, 0)61 }62 // Ensure the reported size is now 0.63 if gotSize := testTreap.Size(); gotSize != 0 {64 t.Fatalf("Size: unexpected byte size - got %d, want 0",65 gotSize,66 )67 }68 // Ensure the treap no longer has any of the keys.69 for i := 0; i < numItems; i++ {70 key := serializeUint32(uint32(i))71 // Ensure the treap no longer has the key.72 if testTreap.Has(key) {73 t.Fatalf("Has #%d: key %q is in treap", i, key)74 }75 // Get the key that no longer exists from the treap and ensure76 // it is nil.77 if gotVal := testTreap.Get(key); gotVal != nil {78 t.Fatalf("Get #%d: unexpected value - got %x, want nil",79 i, gotVal,80 )81 }82 }83 // Ensure the number of keys iterated by ForEach is zero.84 var numIterated int85 testTreap.ForEach(func(k, v []byte) bool {86 numIterated++87 return true88 },89 )90 if numIterated != 0 {91 t.Fatalf("ForEach: unexpected iterate count - got %d, want 0",92 numIterated,93 )94 }95}96// TestMutableSequential ensures that putting keys into a mutable treap in sequential order works as expected.97func TestMutableSequential(t *testing.T) {98 t.Parallel()99 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.100 expectedSize := uint64(0)101 numItems := 1000102 testTreap := NewMutable()103 for i := 0; i < numItems; i++ {104 key := serializeUint32(uint32(i))105 testTreap.Put(key, key)106 // Ensure the treap length is the expected value.107 if gotLen := testTreap.Len(); gotLen != i+1 {108 t.Fatalf("Len #%d: unexpected length - got %d, want %d",109 i, gotLen, i+1,110 )111 }112 // Ensure the treap has the key.113 if !testTreap.Has(key) {114 t.Fatalf("Has #%d: key %q is not in treap", i, key)115 }116 // Get the key from the treap and ensure it is the expected value.117 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {118 t.Fatalf("Get #%d: unexpected value - got %x, want %x",119 i, gotVal, key,120 )121 }122 // Ensure the expected size is reported.123 expectedSize += nodeFieldsSize + 8124 if gotSize := testTreap.Size(); gotSize != expectedSize {125 t.Fatalf("Size #%d: unexpected byte size - got %d, "+126 "want %d", i, gotSize, expectedSize,127 )128 }129 }130 // Ensure the all keys are iterated by ForEach in order.131 var numIterated int132 testTreap.ForEach(func(k, v []byte) bool {133 wantKey := serializeUint32(uint32(numIterated))134 // Ensure the key is as expected.135 if !bytes.Equal(k, wantKey) {136 t.Fatalf("ForEach #%d: unexpected key - got %x, want %x",137 numIterated, k, wantKey,138 )139 }140 // Ensure the value is as expected.141 if !bytes.Equal(v, wantKey) {142 t.Fatalf("ForEach #%d: unexpected value - got %x, want %x",143 numIterated, v, wantKey,144 )145 }146 numIterated++147 return true148 },149 )150 // Ensure all items were iterated.151 if numIterated != numItems {152 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",153 numIterated, numItems,154 )155 }156 // Delete the keys one-by-one while checking several of the treap functions work as expected.157 for i := 0; i < numItems; i++ {158 key := serializeUint32(uint32(i))159 testTreap.Delete(key)160 // Ensure the treap length is the expected value.161 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {162 t.Fatalf("Len #%d: unexpected length - got %d, want %d",163 i, gotLen, numItems-i-1,164 )165 }166 // Ensure the treap no longer has the key.167 if testTreap.Has(key) {168 t.Fatalf("Has #%d: key %q is in treap", i, key)169 }170 // Get the key that no longer exists from the treap and ensure it is nil.171 if gotVal := testTreap.Get(key); gotVal != nil {172 t.Fatalf("Get #%d: unexpected value - got %x, want nil",173 i, gotVal,174 )175 }176 // Ensure the expected size is reported.177 expectedSize -= nodeFieldsSize + 8178 if gotSize := testTreap.Size(); gotSize != expectedSize {179 t.Fatalf("Size #%d: unexpected byte size - got %d, "+180 "want %d", i, gotSize, expectedSize,181 )182 }183 }184}185// TestMutableReverseSequential ensures that putting keys into a mutable treap in reverse sequential order works as186// expected.187func TestMutableReverseSequential(t *testing.T) {188 t.Parallel()189 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.190 expectedSize := uint64(0)191 numItems := 1000192 testTreap := NewMutable()193 for i := 0; i < numItems; i++ {194 key := serializeUint32(uint32(numItems - i - 1))195 testTreap.Put(key, key)196 // Ensure the treap length is the expected value.197 if gotLen := testTreap.Len(); gotLen != i+1 {198 t.Fatalf("Len #%d: unexpected length - got %d, want %d",199 i, gotLen, i+1,200 )201 }202 // Ensure the treap has the key.203 if !testTreap.Has(key) {204 t.Fatalf("Has #%d: key %q is not in treap", i, key)205 }206 // Get the key from the treap and ensure it is the expected value.207 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {208 t.Fatalf("Get #%d: unexpected value - got %x, want %x",209 i, gotVal, key,210 )211 }212 // Ensure the expected size is reported.213 expectedSize += nodeFieldsSize + 8214 if gotSize := testTreap.Size(); gotSize != expectedSize {215 t.Fatalf("Size #%d: unexpected byte size - got %d, "+216 "want %d", i, gotSize, expectedSize,217 )218 }219 }220 // Ensure the all keys are iterated by ForEach in order.221 var numIterated int222 testTreap.ForEach(func(k, v []byte) bool {223 wantKey := serializeUint32(uint32(numIterated))224 // Ensure the key is as expected.225 if !bytes.Equal(k, wantKey) {226 t.Fatalf("ForEach #%d: unexpected key - got %x, want %x",227 numIterated, k, wantKey,228 )229 }230 // Ensure the value is as expected.231 if !bytes.Equal(v, wantKey) {232 t.Fatalf("ForEach #%d: unexpected value - got %x, want %x",233 numIterated, v, wantKey,234 )235 }236 numIterated++237 return true238 },239 )240 // Ensure all items were iterated.241 if numIterated != numItems {242 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",243 numIterated, numItems,244 )245 }246 // Delete the keys one-by-one while checking several of the treap functions work as expected.247 for i := 0; i < numItems; i++ {248 // Intentionally use the reverse order they were inserted here.249 key := serializeUint32(uint32(i))250 testTreap.Delete(key)251 // Ensure the treap length is the expected value.252 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {253 t.Fatalf("Len #%d: unexpected length - got %d, want %d",254 i, gotLen, numItems-i-1,255 )256 }257 // Ensure the treap no longer has the key.258 if testTreap.Has(key) {259 t.Fatalf("Has #%d: key %q is in treap", i, key)260 }261 // Get the key that no longer exists from the treap and ensure it is nil.262 if gotVal := testTreap.Get(key); gotVal != nil {263 t.Fatalf("Get #%d: unexpected value - got %x, want nil",264 i, gotVal,265 )266 }267 // Ensure the expected size is reported.268 expectedSize -= nodeFieldsSize + 8269 if gotSize := testTreap.Size(); gotSize != expectedSize {270 t.Fatalf("Size #%d: unexpected byte size - got %d, "+271 "want %d", i, gotSize, expectedSize,272 )273 }274 }275}276// TestMutableUnordered ensures that putting keys into a mutable treap in no particular order works as expected.277func TestMutableUnordered(t *testing.T) {278 t.Parallel()279 // Insert a bunch of out-of-order keys while checking several of the treap functions work as expected.280 expectedSize := uint64(0)281 numItems := 1000282 testTreap := NewMutable()283 for i := 0; i < numItems; i++ {284 // Hash the serialized int to generate out-of-order keys.285 hash := sha256.Sum256(serializeUint32(uint32(i)))286 key := hash[:]287 testTreap.Put(key, key)288 // Ensure the treap length is the expected value.289 if gotLen := testTreap.Len(); gotLen != i+1 {290 t.Fatalf("Len #%d: unexpected length - got %d, want %d",291 i, gotLen, i+1,292 )293 }294 // Ensure the treap has the key.295 if !testTreap.Has(key) {296 t.Fatalf("Has #%d: key %q is not in treap", i, key)297 }298 // Get the key from the treap and ensure it is the expected299 // value.300 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {301 t.Fatalf("Get #%d: unexpected value - got %x, want %x",302 i, gotVal, key,303 )304 }305 // Ensure the expected size is reported.306 expectedSize += nodeFieldsSize + uint64(len(key)+len(key))307 if gotSize := testTreap.Size(); gotSize != expectedSize {308 t.Fatalf("Size #%d: unexpected byte size - got %d, "+309 "want %d", i, gotSize, expectedSize,310 )311 }312 }313 // Delete the keys one-by-one while checking several of the treap functions work as expected.314 for i := 0; i < numItems; i++ {315 // Hash the serialized int to generate out-of-order keys.316 hash := sha256.Sum256(serializeUint32(uint32(i)))317 key := hash[:]318 testTreap.Delete(key)319 // Ensure the treap length is the expected value.320 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {321 t.Fatalf("Len #%d: unexpected length - got %d, want %d",322 i, gotLen, numItems-i-1,323 )324 }325 // Ensure the treap no longer has the key.326 if testTreap.Has(key) {327 t.Fatalf("Has #%d: key %q is in treap", i, key)328 }329 // Get the key that no longer exists from the treap and ensure it is nil.330 if gotVal := testTreap.Get(key); gotVal != nil {331 t.Fatalf("Get #%d: unexpected value - got %x, want nil",332 i, gotVal,333 )334 }335 // Ensure the expected size is reported.336 expectedSize -= nodeFieldsSize + 64...

Full Screen

Full Screen

api_test.go

Source:api_test.go Github

copy

Full Screen

1// Copyright 2016 The go-ethereum Authors2// This file is part of the go-ethereum library.3//4// The go-ethereum library is free software: you can redistribute it and/or modify5// it under the terms of the GNU Lesser General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.8//9// The go-ethereum library is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU Lesser General Public License for more details.13//14// You should have received a copy of the GNU Lesser General Public License15// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.16package filters17import (18 "encoding/json"19 "fmt"20 "testing"21 "github.com/ethereum/go-ethereum/common"22 "github.com/ethereum/go-ethereum/rpc"23)24func TestUnmarshalJSONNewFilterArgs(t *testing.T) {25 var (26 fromBlock rpc.BlockNumber = 0x12343527 toBlock rpc.BlockNumber = 0xabcdef28 address0 = common.StringToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")29 address1 = common.StringToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")30 topic0 = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")31 topic1 = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")32 topic2 = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")33 )34 // default values35 var test0 FilterCriteria36 if err := json.Unmarshal([]byte("{}"), &test0); err != nil {37 t.Fatal(err)38 }39 if test0.FromBlock != nil {40 t.Fatalf("expected nil, got %d", test0.FromBlock)41 }42 if test0.ToBlock != nil {43 t.Fatalf("expected nil, got %d", test0.ToBlock)44 }45 if len(test0.Addresses) != 0 {46 t.Fatalf("expected 0 addresses, got %d", len(test0.Addresses))47 }48 if len(test0.Topics) != 0 {49 t.Fatalf("expected 0 topics, got %d topics", len(test0.Topics))50 }51 // from, to block number52 var test1 FilterCriteria53 vector := fmt.Sprintf(`{"fromBlock":"0x%x","toBlock":"0x%x"}`, fromBlock, toBlock)54 if err := json.Unmarshal([]byte(vector), &test1); err != nil {55 t.Fatal(err)56 }57 if test1.FromBlock.Int64() != fromBlock.Int64() {58 t.Fatalf("expected FromBlock %d, got %d", fromBlock, test1.FromBlock)59 }60 if test1.ToBlock.Int64() != toBlock.Int64() {61 t.Fatalf("expected ToBlock %d, got %d", toBlock, test1.ToBlock)62 }63 // single address64 var test2 FilterCriteria65 vector = fmt.Sprintf(`{"address": "%s"}`, address0.Hex())66 if err := json.Unmarshal([]byte(vector), &test2); err != nil {67 t.Fatal(err)68 }69 if len(test2.Addresses) != 1 {70 t.Fatalf("expected 1 address, got %d address(es)", len(test2.Addresses))71 }72 if test2.Addresses[0] != address0 {73 t.Fatalf("expected address %x, got %x", address0, test2.Addresses[0])74 }75 // multiple address76 var test3 FilterCriteria77 vector = fmt.Sprintf(`{"address": ["%s", "%s"]}`, address0.Hex(), address1.Hex())78 if err := json.Unmarshal([]byte(vector), &test3); err != nil {79 t.Fatal(err)80 }81 if len(test3.Addresses) != 2 {82 t.Fatalf("expected 2 addresses, got %d address(es)", len(test3.Addresses))83 }84 if test3.Addresses[0] != address0 {85 t.Fatalf("expected address %x, got %x", address0, test3.Addresses[0])86 }87 if test3.Addresses[1] != address1 {88 t.Fatalf("expected address %x, got %x", address1, test3.Addresses[1])89 }90 // single topic91 var test4 FilterCriteria92 vector = fmt.Sprintf(`{"topics": ["%s"]}`, topic0.Hex())93 if err := json.Unmarshal([]byte(vector), &test4); err != nil {94 t.Fatal(err)95 }96 if len(test4.Topics) != 1 {97 t.Fatalf("expected 1 topic, got %d", len(test4.Topics))98 }99 if len(test4.Topics[0]) != 1 {100 t.Fatalf("expected len(topics[0]) to be 1, got %d", len(test4.Topics[0]))101 }102 if test4.Topics[0][0] != topic0 {103 t.Fatalf("got %x, expected %x", test4.Topics[0][0], topic0)104 }105 // test multiple "AND" topics106 var test5 FilterCriteria107 vector = fmt.Sprintf(`{"topics": ["%s", "%s"]}`, topic0.Hex(), topic1.Hex())108 if err := json.Unmarshal([]byte(vector), &test5); err != nil {109 t.Fatal(err)110 }111 if len(test5.Topics) != 2 {112 t.Fatalf("expected 2 topics, got %d", len(test5.Topics))113 }114 if len(test5.Topics[0]) != 1 {115 t.Fatalf("expected 1 topic, got %d", len(test5.Topics[0]))116 }117 if test5.Topics[0][0] != topic0 {118 t.Fatalf("got %x, expected %x", test5.Topics[0][0], topic0)119 }120 if len(test5.Topics[1]) != 1 {121 t.Fatalf("expected 1 topic, got %d", len(test5.Topics[1]))122 }123 if test5.Topics[1][0] != topic1 {124 t.Fatalf("got %x, expected %x", test5.Topics[1][0], topic1)125 }126 // test optional topic127 var test6 FilterCriteria128 vector = fmt.Sprintf(`{"topics": ["%s", null, "%s"]}`, topic0.Hex(), topic2.Hex())129 if err := json.Unmarshal([]byte(vector), &test6); err != nil {130 t.Fatal(err)131 }132 if len(test6.Topics) != 3 {133 t.Fatalf("expected 3 topics, got %d", len(test6.Topics))134 }135 if len(test6.Topics[0]) != 1 {136 t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0]))137 }138 if test6.Topics[0][0] != topic0 {139 t.Fatalf("got %x, expected %x", test6.Topics[0][0], topic0)140 }141 if len(test6.Topics[1]) != 0 {142 t.Fatalf("expected 0 topic, got %d", len(test6.Topics[1]))143 }144 if len(test6.Topics[2]) != 1 {145 t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2]))146 }147 if test6.Topics[2][0] != topic2 {148 t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2)149 }150 // test OR topics151 var test7 FilterCriteria152 vector = fmt.Sprintf(`{"topics": [["%s", "%s"], null, ["%s", null]]}`, topic0.Hex(), topic1.Hex(), topic2.Hex())153 if err := json.Unmarshal([]byte(vector), &test7); err != nil {154 t.Fatal(err)155 }156 if len(test7.Topics) != 3 {157 t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics))158 }159 if len(test7.Topics[0]) != 2 {160 t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0]))161 }162 if test7.Topics[0][0] != topic0 || test7.Topics[0][1] != topic1 {163 t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]",164 topic0, topic1, test7.Topics[0][0], test7.Topics[0][1],165 )166 }167 if len(test7.Topics[1]) != 0 {168 t.Fatalf("expected 0 topic, got %d topics", len(test7.Topics[1]))169 }170 if len(test7.Topics[2]) != 0 {171 t.Fatalf("expected 0 topics, got %d topics", len(test7.Topics[2]))172 }173}...

Full Screen

Full Screen

Len

Using AI Code Generation

copy

Full Screen

1func main() {2}3func main() {4}5func main() {6}7func main() {8}9func main() {10}11func main() {12}13func main() {14}15func main() {16}17func main() {18}19func main() {20}21func main() {22}23func main() {24}25func main() {26}27func main() {28}29func main() {30}31func main()

Full Screen

Full Screen

Len

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 got := []int{1, 2, 3, 4, 5}4 fmt.Println(len(got))5}6len(array)7import (8func main() {9 got := [5]int{1, 2, 3, 4, 5}10 fmt.Println(len(got))11}12len(array)13import (14func main() {15 got := [5]int{1, 2, 3, 4, 5}16 fmt.Println(len(got))17}18len(array)19import (20func main() {21 got := [...]int{1, 2, 3, 4, 5}22 fmt.Println(len(got))23}24len(array)25import (26func main() {27 got := [...]int{1, 2, 3, 4, 5}28 fmt.Println(len(got))29}30len(array)

Full Screen

Full Screen

Len

Using AI Code Generation

copy

Full Screen

1import (2func main() {3a = got{1, 2}4fmt.Println(a.Len())5}6import (7func main() {8a = got{1, 2}9fmt.Println(a.Len())10}11import (12func main() {13a = got{1, 2}14fmt.Println(a.Len())15}16import (17type got interface {18Len() float6419}20func main() {21a = got{1, 2}22fmt.Println(a.Len())23}24import (25type got interface {26Len() float6427}28func main() {29a = got{1, 2}30fmt.Println(a.Len())31}32import (33type got interface {34Len() float6435}36func main() {37a = got{1, 2}38fmt.Println(a.Len())39}

Full Screen

Full Screen

Len

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main(){3got := []int{1,2,3}4fmt.Println(len(got))5}6import "fmt"7func main(){8got := []int{1,2,3}9fmt.Println(got.Len())10}11A method can be defined on pointer receivers. This means the receiver type has the literal syntax *T for some type T. (Also, T cannot itself be a pointer such as *int.)12import "fmt"13func (g got) Len() int {14return len(g)15}16func main(){17got := got{1,2,3}18fmt.Println(got.Len())19}20import "fmt"21func (g *got) Len() int {22return len(*g)23}24func main(){25got := got{1,2,3}26fmt.Println(got.Len())27}

Full Screen

Full Screen

Len

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Printf("T/F? Does the string \"%s\" have prefix \"Hello\"?4 fmt.Printf("%t5", strings.HasPrefix(str, "Hello"))6 fmt.Printf("T/F? Does the string \"%s\" have prefix \"How\"?7 fmt.Printf("%t8", strings.HasPrefix(str, "How"))9}10import (11func main() {12 fmt.Printf("T/F? Does the string \"%s\" have prefix \"Hello\"?13 fmt.Printf("%t14", strings.HasPrefix(str, "Hello"))15 fmt.Printf("T/F? Does the string \"%s\" have prefix \"How\"?16 fmt.Printf("%t17", strings.HasPrefix(str, "How"))18}19import (20func main() {21 fmt.Printf("T/F? Does the string \"%s\" have prefix \"Hello\"?22 fmt.Printf("%t23", strings.HasPrefix(str, "Hello"))24 fmt.Printf("T/F? Does the string \"%s\" have prefix \"How\"?25 fmt.Printf("%t26", strings.HasPrefix(str, "How"))27}28import (29func main() {30 fmt.Printf("T/F? Does the string \"%s\" have prefix

Full Screen

Full Screen

Len

Using AI Code Generation

copy

Full Screen

1import (2func main() {3got := got.Got{}4fmt.Printf("Length of got is %d5}6import (7func main() {8got := got.Got{}9got.SetLen(5)10fmt.Printf("Length of got is %d11}12type Got struct {13}14func (g *Got) SetLen(l int) {15}16import (17func main() {18got := got.Got{}19got.SetLen(5)20fmt.Printf("Length of got is %d21}22type Got struct {23}24func (g Got) SetLen(l int) {25}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful