summaryrefslogtreecommitdiff
path: root/Examples/test-suite/perl5/li_constraints_runme.pl
blob: b1fd143e20777307c09c1966905399a0516504a4 (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
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 27;
BEGIN { use_ok('li_constraints') }
require_ok('li_constraints');

sub check_double {
    my ($except, $func, $name, $val) = @_;
    my $fname = "li_constraints::test_$name";
    $fname =~ s/-//;
    my $actual = eval { $func->($val); 1; };
    my $err = $@;
    $actual = 0 unless defined $actual;
    if($actual) {
      is($actual, $except, "$fname pass with $val");
    } else {
      is($actual, $except, "$fname throw exception with $val");
      ok($err =~ "^ValueError Expected a $name value.", "$fname throw proper exception");
    }
}

my $nonnegative = sub { li_constraints::test_nonnegative(shift); };
check_double(1, $nonnegative, "non-negative", 10);
check_double(1, $nonnegative, "non-negative", 0);
check_double(0, $nonnegative, "non-negative", -10);

my $nonpositive = sub { li_constraints::test_nonpositive(shift); };
check_double(0, $nonpositive, "non-positive", 10);
check_double(1, $nonpositive, "non-positive", 0);
check_double(1, $nonpositive, "non-positive", -10);

my $positive = sub { li_constraints::test_positive(shift); };
check_double(1, $positive, "positive", 10);
check_double(0, $positive, "positive", 0);
check_double(0, $positive, "positive", -10);

my $negative = sub { li_constraints::test_negative(shift); };
check_double(0, $negative, "negative", 10);
check_double(0, $negative, "negative", 0);
check_double(1, $negative, "negative", -10);

my $nonzero = sub { li_constraints::test_nonzero(shift); };
check_double(1, $nonzero, "nonzero", 10);
check_double(0, $nonzero, "nonzero", 0);
check_double(1, $nonzero, "nonzero", -10);

# Pass null value
my $ret = eval { li_constraints::test_nonnull(undef); 1; };
my $err = $@;
is($ret, undef, "li_constraints::test_nonnull throw exception with null");
ok($err =~ "^ValueError Received a NULL pointer.", "li_constraints::test_nonnull throw proper exception");
my $ptr = li_constraints::get_nonnull();
# There should be no exception, we can use Perl lambda function
$ret = (sub { li_constraints::test_nonnull($ptr); 1; })->();
is($ret, 1, "li_constraints::test_nonnull pass with non null value");