diff options
| author | Andrew Dunham <andrew@du.nham.ca> | 2023-02-21 14:01:07 -0500 |
|---|---|---|
| committer | Andrew Dunham <andrew@tailscale.com> | 2023-02-21 19:11:08 +0000 |
| commit | e220fa65dd03b417ace0eb22d372d342fceb183c (patch) | |
| tree | e4ca58034637342182eb9d709cb490a03d790cf8 /util/ringbuffer/ringbuffer_test.go | |
| parent | cd18bb68a49608b86f60e22cb081f0156d3d11b5 (diff) | |
| download | tailscale-e220fa65dd03b417ace0eb22d372d342fceb183c.tar.xz tailscale-e220fa65dd03b417ace0eb22d372d342fceb183c.zip | |
util/ringbuffer: move generic ringbuffer from corp repo
Also add some basic tests for this implementation.
Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: I307ebb6db91d0c172657befb276b38ccb638f828
Diffstat (limited to 'util/ringbuffer/ringbuffer_test.go')
| -rw-r--r-- | util/ringbuffer/ringbuffer_test.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/util/ringbuffer/ringbuffer_test.go b/util/ringbuffer/ringbuffer_test.go new file mode 100644 index 000000000..fae3f5328 --- /dev/null +++ b/util/ringbuffer/ringbuffer_test.go @@ -0,0 +1,55 @@ +// Copyright (c) Tailscale Inc & AUTHORS +// SPDX-License-Identifier: BSD-3-Clause + +package ringbuffer + +import ( + "reflect" + "testing" +) + +func TestRingBuffer(t *testing.T) { + const numItems = 10 + rb := New[int](numItems) + + for i := 0; i < numItems-1; i++ { + rb.Add(i) + } + + t.Run("NotFull", func(t *testing.T) { + if ll := rb.Len(); ll != numItems-1 { + t.Fatalf("got len %d; want %d", ll, numItems-1) + } + all := rb.GetAll() + want := []int{0, 1, 2, 3, 4, 5, 6, 7, 8} + if !reflect.DeepEqual(all, want) { + t.Fatalf("items mismatch\ngot: %v\nwant %v", all, want) + } + }) + + t.Run("Full", func(t *testing.T) { + // Append items to evict something + rb.Add(98) + rb.Add(99) + + if ll := rb.Len(); ll != numItems { + t.Fatalf("got len %d; want %d", ll, numItems) + } + all := rb.GetAll() + want := []int{1, 2, 3, 4, 5, 6, 7, 8, 98, 99} + if !reflect.DeepEqual(all, want) { + t.Fatalf("items mismatch\ngot: %v\nwant %v", all, want) + } + }) + + t.Run("Clear", func(t *testing.T) { + rb.Clear() + if ll := rb.Len(); ll != 0 { + t.Fatalf("got len %d; want 0", ll) + } + all := rb.GetAll() + if len(all) != 0 { + t.Fatalf("got non-empty list; want empty") + } + }) +} |
