1
exercism fetch lua nth-prime

nth-prime_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
local nth = require('nth-prime')

describe('nth-prime', function()
  local function benchmark(f)
    local start = os.clock()
    f()
    return os.clock() - start
  end

  it('should give 2 as the first prime', function()
    assert.equal(2, nth(1))
  end)

  it('should give 3 as the second prime', function()
    assert.equal(3, nth(2))
  end)

  it('should be able to calculate the nth prime for small n', function()
    assert.equal(13, nth(6))
  end)

  it('should be able to calculate the nth prime for large n', function()
    assert.equal(104743, nth(10001))
  end)

  it('should be efficient for large n', function()
    local execution_time = benchmark(function()
      nth(10001)
    end)

    assert(execution_time < 1, 'should take less than a second to execute')
  end)

  it('should raise an error for n <= 0', function()
    assert.has_error(function()
      nth(0)
    end)

    assert.has_error(function()
      nth(-1)
    end)
  end)
end)