<commit_before>package cbuf_test

import (
	"testing"

	"github.com/alcortesm/queue"
	"github.com/alcortesm/queue/cbuf"
	"github.com/alcortesm/queue/check"
)

func mustNew(t *testing.T, n int) queue.Queue {
	q, err := cbuf.New(n)
	if err != nil {
		t.Errorf("creating cbuf of size %d: %s", n, err)
	}
	var i interface{} = q
	if _, ok := i.(queue.Queue); !ok {
		t.Errorf("cbuf does not implement queue.Queue")
	}
	return q
}

func TestIsBounded(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
		expected bool
	}{
		{"zero", 0, true},
		{"one", 1, true},
		{"two", 2, true},
		{"ten", 10, true},
	} {
		check.IsBounded(t,
			mustNew(t, test.capacity),
			test.expected,
			test.context)
	}
}

func TestCap(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
	}{
		{"zero", 0},
		{"one", 1},
		{"two", 2},
		{"ten", 10},
	} {
		expected := test.capacity
		check.CapBounded(t,
			mustNew(t, test.capacity),
			expected,
			test.context)
	}
}

func TestEmptyIsCorrectlyDetected(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
	}{
		{"one", 0},
		{"one", 1},
		{"two", 2},
		{"ten", 10},
	} {
		q := mustNew(t, test.capacity)
		// it must be empty initially...
		check.IsEmpty(t, q, true, test.context)
		check.Len(t, q, 0, test.context)
		check.HeadErrEmpty(t, q, test.context)
		check.DequeueErrEmpty(t, q, test.context)
		// and also if filled and depleted...
		check.FillEmptyWithNumbers(t, q, test.context)
		check.DepleteFullExpectingNumbers(t, q, test.context)
		check.IsEmpty(t, q, true, test.context)
		check.Len(t, q, 0, test.context)
		check.HeadErrEmpty(t, q, test.context)
		check.DequeueErrEmpty(t, q, test.context)
		// and even if filled and depleted a second time...
		check.FillEmptyWithNumbers(t, q, test.context)
		check.DepleteFullExpectingNumbers(t, q, test.context)
		check.IsEmpty(t, q, true, test.context)
		check.Len(t, q, 0, test.context)
		check.HeadErrEmpty(t, q, test.context)
		check.DequeueErrEmpty(t, q, test.context)
	}
}

func TestInitiallyAreNotFull(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
		expected bool
	}{
		{"zero", 0, true}, // zero capacity bounded queues are always full
		{"one", 1, false},
		{"two", 2, false},
		{"ten", 10, false},
	} {
		check.IsFull(t,
			mustNew(t, test.capacity),
			test.expected,
			test.context)
	}
}

func TestEnqueueErrFull(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
	}{
		{"zero", 0},
		{"one", 1},
		{"two", 2},
		{"ten", 10},
	} {
		q := mustNew(t, test.capacity)
		check.FillEmptyWithNumbers(t, q, test.context)
		check.EnqueueErrFull(t, q, test.context)
	}
}

func TestHeadOKWhileFillingUpAndDepleting(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
	}{
		{"zero", 0},
		{"one", 1},
		{"two", 2},
		{"ten", 10},
	} {
		check.HeadOKWhileFillingUpAndDepleting(t,
			mustNew(t, test.capacity),
			test.context)
	}<commit_msg>cbuf: better checks of full queues<commit_after>package cbuf_test

import (
	"testing"

	"github.com/alcortesm/queue"
	"github.com/alcortesm/queue/cbuf"
	"github.com/alcortesm/queue/check"
)

func mustNew(t *testing.T, n int) queue.Queue {
	q, err := cbuf.New(n)
	if err != nil {
		t.Errorf("creating cbuf of size %d: %s", n, err)
	}
	var i interface{} = q
	if _, ok := i.(queue.Queue); !ok {
		t.Errorf("cbuf does not implement queue.Queue")
	}
	return q
}

func TestIsBounded(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
		expected bool
	}{
		{"zero", 0, true},
		{"one", 1, true},
		{"two", 2, true},
		{"ten", 10, true},
	} {
		check.IsBounded(t,
			mustNew(t, test.capacity),
			test.expected,
			test.context)
	}
}

func TestCap(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
	}{
		{"zero", 0},
		{"one", 1},
		{"two", 2},
		{"ten", 10},
	} {
		expected := test.capacity
		check.CapBounded(t,
			mustNew(t, test.capacity),
			expected,
			test.context)
	}
}

func TestEmptyIsCorrectlyDetected(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
	}{
		{"one", 0},
		{"one", 1},
		{"two", 2},
		{"ten", 10},
	} {
		q := mustNew(t, test.capacity)
		// it must be empty initially...
		check.IsEmpty(t, q, true, test.context)
		check.Len(t, q, 0, test.context)
		check.HeadErrEmpty(t, q, test.context)
		check.DequeueErrEmpty(t, q, test.context)
		// and also if filled and depleted...
		check.FillEmptyWithNumbers(t, q, test.context)
		check.DepleteFullExpectingNumbers(t, q, test.context)
		check.IsEmpty(t, q, true, test.context)
		check.Len(t, q, 0, test.context)
		check.HeadErrEmpty(t, q, test.context)
		check.DequeueErrEmpty(t, q, test.context)
		// and even if filled and depleted a second time...
		check.FillEmptyWithNumbers(t, q, test.context)
		check.DepleteFullExpectingNumbers(t, q, test.context)
		check.IsEmpty(t, q, true, test.context)
		check.Len(t, q, 0, test.context)
		check.HeadErrEmpty(t, q, test.context)
		check.DequeueErrEmpty(t, q, test.context)
	}
}

func TestInitiallyAreNotFull(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
		expected bool
	}{
		{"zero", 0, true}, // zero capacity bounded queues are always full
		{"one", 1, false},
		{"two", 2, false},
		{"ten", 10, false},
	} {
		check.IsFull(t,
			mustNew(t, test.capacity),
			test.expected,
			test.context)
	}
}

func TestFull(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
	}{
		{"zero", 0},
		{"one", 1},
		{"two", 2},
		{"ten", 10},
	} {
		q := mustNew(t, test.capacity)
		check.FillEmptyWithNumbers(t, q, test.context)
		check.IsFull(t, q, true, test.context)
		check.EnqueueErrFull(t, q, test.context)
	}
}

func TestHeadOKWhileFillingUpAndDepleting(t *testing.T) {
	for _, test := range []struct {
		context  string
		capacity int
	}{
		{"zero", 0},
		{"one", 1},
		{"two", 2},
		{"ten", 10},
	} {
		check.HeadOKWhileFillingUpAndDepleting(t,
			mustNew(t, test.capacity),
			test.context)
	}
{'fraction_non_alphanumeric': 0.17139090309822017, 'fraction_numerical': 0.010876730388925511, 'mean_word_length': 3.4955555555555557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4846595', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2355, 'n_words': 573}