File: sequence_test.go

package info (click to toggle)
golang-github-segmentio-ksuid 1.0.4-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 168 kB
  • sloc: makefile: 2
file content (33 lines) | stat: -rw-r--r-- 721 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package ksuid

import (
	"encoding/binary"
	"math"
	"testing"
)

func TestSequence(t *testing.T) {
	seq := Sequence{Seed: New()}

	if min, max := seq.Bounds(); min == max {
		t.Error("min and max of KSUID range must differ when no ids have been generated")
	}

	for i := 0; i <= math.MaxUint16; i++ {
		id, err := seq.Next()
		if err != nil {
			t.Fatal(err)
		}
		if j := int(binary.BigEndian.Uint16(id[len(id)-2:])); j != i {
			t.Fatalf("expected %d but got %d in %s", i, j, id)
		}
	}

	if _, err := seq.Next(); err == nil {
		t.Fatal("no error returned after exhausting the id generator")
	}

	if min, max := seq.Bounds(); min != max {
		t.Error("after all KSUIDs were generated the min and max must be equal")
	}
}