aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/ruby/li_constraints_runme.rb
blob: a4dcd685ac77c8d2f5a0dda502b91928a7d1dc44 (plain)
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
#!/usr/bin/env ruby

require 'li_constraints'
include Li_constraints

def check_double(except, fn, f, val)
  actual = true
  proper = false
  begin
    fn.call(val)
  rescue => e
    actual = false
    proper = e.class == ArgumentError && e.message == "Expected a #{f} value."
  end
  if actual
    if !except
      raise RuntimeError, "function '#{f}' with #{val} should perform an exception"
    end
  else
    if except
      raise RuntimeError, "function '#{f}' with #{val} should not perform an exception"
    elsif !proper
      raise RuntimeError, "function '#{f}' with #{val} should perform a proper exception"
    end
  end
end

nonnegative = -> (v) { test_nonnegative(v) }
check_double(true, nonnegative, "non-negative", 10)
check_double(true, nonnegative, "non-negative", 0)
check_double(false, nonnegative, "non-negative", -10)

nonpositive = -> (v) { test_nonpositive(v) }
check_double(false, nonpositive, "non-positive", 10)
check_double(true, nonpositive, "non-positive", 0)
check_double(true, nonpositive, "non-positive", -10)

positive = -> (v) { test_positive(v) }
check_double(true, positive, "positive", 10)
check_double(false, positive, "positive", 0)
check_double(false, positive, "positive", -10)

negative = -> (v) { test_negative(v) }
check_double(false, negative, "negative", 10)
check_double(false, negative, "negative", 0)
check_double(true, negative, "negative", -10)

nonzero = -> (v) { test_nonzero(v) }
check_double(true, nonzero, "nonzero", 10)
check_double(false, nonzero, "nonzero", 0)
check_double(true, nonzero, "nonzero", -10)

have_exception = false
begin
  test_nonnull(nil)
rescue => e
  have_exception = e.class == ArgumentError && e.message == "Received a NULL pointer."
end
if not have_exception
  raise RuntimeError, "test_nonnull should perform exception with 'null' value"
end
nonnull = get_nonnull()
test_nonnull(nonnull)