Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

sieve_test.gno

1.39 Kb · 55 lines
 1package sieve
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestPrimesUpTo(t *testing.T) {
10	cases := []struct {
11		name string
12		n    int
13		want []int
14	}{
15		{"below two", 1, []int{}},
16		{"exactly two", 2, []int{2}},
17		{"ten", 10, []int{2, 3, 5, 7}},
18		{"thirty", 30, []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}},
19	}
20
21	for _, tc := range cases {
22		got := PrimesUpTo(tc.n)
23		uassert.Equal(t, len(tc.want), len(got), tc.name+": length")
24		for i := range tc.want {
25			if i >= len(got) {
26				break
27			}
28			uassert.Equal(t, tc.want[i], got[i], tc.name+": element")
29		}
30	}
31
32	// π(100) == 25, π(1000) == 168 — classic sieve landmarks.
33	uassert.Equal(t, 25, len(PrimesUpTo(100)), "pi(100)")
34	uassert.Equal(t, 168, len(PrimesUpTo(1000)), "pi(1000)")
35
36	// clamping: anything above MaxN yields the same set as MaxN.
37	uassert.Equal(t, len(PrimesUpTo(MaxN)), len(PrimesUpTo(MaxN+5000)), "clamp")
38}
39
40func TestIsPrimeAndNthPrime(t *testing.T) {
41	primeCases := []int{2, 3, 5, 7, 11, 13, 9973}
42	for _, p := range primeCases {
43		uassert.True(t, IsPrime(p), "expected prime")
44	}
45
46	compositeCases := []int{0, 1, 4, 9, 100, 9975}
47	for _, c := range compositeCases {
48		uassert.False(t, IsPrime(c), "expected non-prime")
49	}
50
51	uassert.Equal(t, 2, NthPrime(1), "1st prime")
52	uassert.Equal(t, 29, NthPrime(10), "10th prime")
53	uassert.Equal(t, 541, NthPrime(100), "100th prime")
54	uassert.Equal(t, 0, NthPrime(0), "0th prime is invalid")
55}