mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-28 07:59:57 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
14
vendor/zombiezen.com/go/capnproto2/internal/queue/BUILD.bazel
generated
vendored
Normal file
14
vendor/zombiezen.com/go/capnproto2/internal/queue/BUILD.bazel
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["queue.go"],
|
||||
importpath = "zombiezen.com/go/capnproto2/internal/queue",
|
||||
visibility = ["//:__subpackages__"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["queue_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
)
|
70
vendor/zombiezen.com/go/capnproto2/internal/queue/queue.go
generated
vendored
Normal file
70
vendor/zombiezen.com/go/capnproto2/internal/queue/queue.go
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
// Package queue implements a generic queue using a ring buffer.
|
||||
package queue
|
||||
|
||||
// A Queue wraps an Interface to provide queue operations.
|
||||
type Queue struct {
|
||||
q Interface
|
||||
start int
|
||||
n int
|
||||
cap int
|
||||
}
|
||||
|
||||
// New creates a new queue that starts with n elements. The interface's
|
||||
// length must not change over the course of the queue's usage.
|
||||
func New(q Interface, n int) *Queue {
|
||||
qq := new(Queue)
|
||||
qq.Init(q, n)
|
||||
return qq
|
||||
}
|
||||
|
||||
// Init initializes a queue. The old queue is untouched.
|
||||
func (q *Queue) Init(r Interface, n int) {
|
||||
q.q = r
|
||||
q.start = 0
|
||||
q.n = n
|
||||
q.cap = r.Len()
|
||||
}
|
||||
|
||||
// Len returns the length of the queue. This is different from the
|
||||
// underlying interface's length, which is the queue's capacity.
|
||||
func (q *Queue) Len() int {
|
||||
return q.n
|
||||
}
|
||||
|
||||
// Push reserves space for an element on the queue, returning its index.
|
||||
// If the queue is full, Push returns -1.
|
||||
func (q *Queue) Push() int {
|
||||
if q.n >= q.cap {
|
||||
return -1
|
||||
}
|
||||
i := (q.start + q.n) % q.cap
|
||||
q.n++
|
||||
return i
|
||||
}
|
||||
|
||||
// Front returns the index of the front of the queue, or -1 if the queue is empty.
|
||||
func (q *Queue) Front() int {
|
||||
if q.n == 0 {
|
||||
return -1
|
||||
}
|
||||
return q.start
|
||||
}
|
||||
|
||||
// Pop pops an element from the queue, returning whether it succeeded.
|
||||
func (q *Queue) Pop() bool {
|
||||
if q.n == 0 {
|
||||
return false
|
||||
}
|
||||
q.q.Clear(q.start)
|
||||
q.start = (q.start + 1) % q.cap
|
||||
q.n--
|
||||
return true
|
||||
}
|
||||
|
||||
// A type implementing Interface can be used to store elements in a Queue.
|
||||
type Interface interface {
|
||||
// Len returns the number of elements available.
|
||||
Len() int
|
||||
// Clear removes the element at i.
|
||||
Clear(i int)
|
||||
}
|
154
vendor/zombiezen.com/go/capnproto2/internal/queue/queue_test.go
generated
vendored
Normal file
154
vendor/zombiezen.com/go/capnproto2/internal/queue/queue_test.go
generated
vendored
Normal file
@@ -0,0 +1,154 @@
|
||||
package queue
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
qi := make(ints, 5)
|
||||
|
||||
q := New(qi, 0)
|
||||
|
||||
if n := q.Len(); n != 0 {
|
||||
t.Errorf("New(qi, 0).Len() = %d; want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepush(t *testing.T) {
|
||||
qi := make(ints, 5)
|
||||
qi[0] = 42
|
||||
|
||||
q := New(qi, 1)
|
||||
|
||||
if n := q.Len(); n != 1 {
|
||||
t.Fatalf("New(qi, 1).Len() = %d; want 1", n)
|
||||
}
|
||||
if i := q.Front(); i != 0 {
|
||||
t.Errorf("q.Front() = %d; want 0", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPush(t *testing.T) {
|
||||
qi := make(ints, 5)
|
||||
q := New(qi, 0)
|
||||
|
||||
i := q.Push()
|
||||
if i == -1 {
|
||||
t.Error("q.Push() returned -1")
|
||||
}
|
||||
qi[i] = 42
|
||||
|
||||
if n := q.Len(); n != 1 {
|
||||
t.Errorf("q.Len() after push = %d; want 1", n)
|
||||
}
|
||||
if front := q.Front(); front != i {
|
||||
t.Errorf("q.Front() after push = %d; want %d", front, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushFull(t *testing.T) {
|
||||
qi := make(ints, 5)
|
||||
q := New(qi, 0)
|
||||
var ok [6]bool
|
||||
|
||||
push := func(n int, val int) {
|
||||
i := q.Push()
|
||||
if i == -1 {
|
||||
return
|
||||
}
|
||||
ok[n] = true
|
||||
qi[i] = val
|
||||
}
|
||||
push(0, 10)
|
||||
push(1, 11)
|
||||
push(2, 12)
|
||||
push(3, 13)
|
||||
push(4, 14)
|
||||
push(5, 15)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if !ok[i] {
|
||||
t.Errorf("q.Push() #%d returned -1", i)
|
||||
}
|
||||
}
|
||||
if ok[5] {
|
||||
t.Error("q.Push() #5 returned true")
|
||||
}
|
||||
if n := q.Len(); n != 5 {
|
||||
t.Errorf("q.Len() after full = %d; want 5", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPop(t *testing.T) {
|
||||
qi := make(ints, 5)
|
||||
q := New(qi, 0)
|
||||
qi[q.Push()] = 1
|
||||
qi[q.Push()] = 2
|
||||
qi[q.Push()] = 3
|
||||
|
||||
outs := make([]int, 3)
|
||||
for n := range outs {
|
||||
i := q.Front()
|
||||
if i == -1 {
|
||||
t.Fatalf("before q.Pop() #%d, Front == -1", n)
|
||||
}
|
||||
outs[n] = qi[i]
|
||||
if !q.Pop() {
|
||||
t.Fatalf("q.Pop() #%d = false", n)
|
||||
}
|
||||
}
|
||||
|
||||
if n := q.Len(); n != 0 {
|
||||
t.Errorf("q.Len() after pops = %d; want 0", n)
|
||||
}
|
||||
if outs[0] != 1 {
|
||||
t.Errorf("pop #0 = %d; want 1", outs[0])
|
||||
}
|
||||
if outs[1] != 2 {
|
||||
t.Errorf("pop #1 = %d; want 2", outs[1])
|
||||
}
|
||||
if outs[2] != 3 {
|
||||
t.Errorf("pop #2 = %d; want 3", outs[2])
|
||||
}
|
||||
for i := range qi {
|
||||
if qi[i] != 0 {
|
||||
t.Errorf("qi[%d] = %d; want 0 (not cleared)", i, qi[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrap(t *testing.T) {
|
||||
qi := make(ints, 5)
|
||||
q := New(qi, 0)
|
||||
|
||||
qi[q.Push()] = 10
|
||||
qi[q.Push()] = 11
|
||||
qi[q.Push()] = 12
|
||||
q.Pop()
|
||||
q.Pop()
|
||||
qi[q.Push()] = 13
|
||||
qi[q.Push()] = 14
|
||||
qi[q.Push()] = 15
|
||||
qi[q.Push()] = 16
|
||||
|
||||
if n := q.Len(); n != 5 {
|
||||
t.Errorf("q.Len() = %d; want 5", n)
|
||||
}
|
||||
for i := 12; q.Len() > 0; i++ {
|
||||
if x := qi[q.Front()]; x != i {
|
||||
t.Errorf("qi[q.Front()] = %d; want %d", x, i)
|
||||
}
|
||||
if !q.Pop() {
|
||||
t.Error("q.Pop() returned false")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ints []int
|
||||
|
||||
func (is ints) Len() int {
|
||||
return len(is)
|
||||
}
|
||||
|
||||
func (is ints) Clear(i int) {
|
||||
is[i] = 0
|
||||
}
|
Reference in New Issue
Block a user