Source file src/internal/fuzz/queue_test.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fuzz
     6  
     7  import "testing"
     8  
     9  func TestQueue(t *testing.T) {
    10  	// Zero valued queue should have 0 length and capacity.
    11  	var q queue
    12  	if n := q.len; n != 0 {
    13  		t.Fatalf("empty queue has len %d; want 0", n)
    14  	}
    15  	if n := q.cap(); n != 0 {
    16  		t.Fatalf("empty queue has cap %d; want 0", n)
    17  	}
    18  
    19  	// As we add elements, len should grow.
    20  	N := 32
    21  	for i := 0; i < N; i++ {
    22  		q.enqueue(i)
    23  		if n := q.len; n != i+1 {
    24  			t.Fatalf("after adding %d elements, queue has len %d", i, n)
    25  		}
    26  		if v, ok := q.peek(); !ok {
    27  			t.Fatalf("couldn't peek after adding %d elements", i)
    28  		} else if v.(int) != 0 {
    29  			t.Fatalf("after adding %d elements, peek is %d; want 0", i, v)
    30  		}
    31  	}
    32  
    33  	// As we remove and add elements, len should shrink and grow.
    34  	// We should also remove elements in the same order they were added.
    35  	want := 0
    36  	for _, r := range []int{1, 2, 3, 5, 8, 13, 21} {
    37  		s := make([]int, 0, r)
    38  		for i := 0; i < r; i++ {
    39  			if got, ok := q.dequeue(); !ok {
    40  				t.Fatalf("after removing %d of %d elements, could not dequeue", i+1, r)
    41  			} else if got != want {
    42  				t.Fatalf("after removing %d of %d elements, got %d; want %d", i+1, r, got, want)
    43  			} else {
    44  				s = append(s, got.(int))
    45  			}
    46  			want = (want + 1) % N
    47  			if n := q.len; n != N-i-1 {
    48  				t.Fatalf("after removing %d of %d elements, len is %d; want %d", i+1, r, n, N-i-1)
    49  			}
    50  		}
    51  		for i, v := range s {
    52  			q.enqueue(v)
    53  			if n := q.len; n != N-r+i+1 {
    54  				t.Fatalf("after adding back %d of %d elements, len is %d; want %d", i+1, r, n, n-r+i+1)
    55  			}
    56  		}
    57  	}
    58  }
    59  

View as plain text