-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes_spec.lua
102 lines (96 loc) · 3.37 KB
/
primes_spec.lua
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env busted
local primes = require "primes"
describe("Primes.factor", function()
it("should factorize a positive number correctly", function()
assert.equal("2^2 * 5^2", tostring(primes.factor(100)))
end)
it("should factorize a negative number correctly", function()
assert.equal("-1 * 3^2", tostring(primes.factor(-9)))
end)
it("should factorize zero correctly", function()
assert.equal("0", tostring(primes.factor(0)))
end)
it("should factorize 511 correctly", function()
assert.equal("7 * 73", tostring(primes.factor(511)))
end)
it("should round trip a positive number correctly", function ()
assert.equal(100, primes.factor(100):tonumber())
end)
it("should round trip a negative number correctly", function ()
assert.equal(-9, primes.factor(-9):tonumber())
end)
it("should round trip zero correctly", function ()
assert.equal(0, primes.factor(0):tonumber())
end)
end)
describe("Primes.nextprime", function()
it("should find the next prime", function()
assert.equal(5, primes.nextprime(4))
assert.equal(5, primes.nextprime(5))
assert.equal(7, primes.nextprime(4, 2))
end)
end)
describe("Primes.prevprime", function()
it("should find the previous prime", function()
assert.equal(3, primes.prevprime(4))
assert.equal(5, primes.prevprime(5))
assert.equal(3, primes.prevprime(5, 2))
end)
end)
describe("Primes.prime", function()
it("should find the 1st prime number.", function()
assert.equal(2, primes.prime(1))
end)
it("should find the 2nd prime number.", function()
assert.equal(3, primes.prime(2))
end)
it("should find the 3rd prime number.", function()
assert.equal(5, primes.prime(3))
end)
it("should find the 65th prime number.", function()
assert.equal(313, primes.prime(65))
end)
it("should find the 10000th prime", function ()
assert.equal(104729, primes.prime(10000))
end)
it("should find the 2000th prime number.", function()
assert.equal(17389, primes.prime(2000))
end)
it("should find the 1000th prime number.", function()
assert.equal(7919, primes.prime(1000))
end)
end)
describe("Primes.isprime", function()
it("should determine 1 is not prime", function()
assert.is_false(primes.isprime(1))
end)
it("should determine 2 is prime", function()
assert.is_true(primes.isprime(2))
end)
it("should determine 3 is prime", function()
assert.is_true(primes.isprime(3))
end)
it("should determine 4 is not prime", function()
assert.is_false(primes.isprime(4))
end)
it("should determine 5 is prime", function()
assert.is_true(primes.isprime(5))
end)
it("should determine 9 is not prime", function()
assert.is_false(primes.isprime(9))
end)
it("should determine 291 is not prime", function()
assert.is_false(primes.isprime(291))
end)
it("should determine 293 is prime", function()
assert.is_true(primes.isprime(293))
end)
end)
describe("Primes.radical", function()
it("should compute the radical of a positive number", function()
assert.equal(6, primes.radical(2 * 2 * 3))
end)
it("should compute the radical of a negative number", function()
assert.equal(-3, primes.radical(-9))
end)
end)