PageRenderTime 42ms CodeModel.GetById 16ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/perl5/operator_overload_runme.pl

#
Perl | 175 lines | 104 code | 37 blank | 34 comment | 4 complexity | 60e878d6fbb668b1c6e3a226f87966ff MD5 | raw file
  1#!/usr/bin/perl -w
  2use strict;
  3use Test::More tests => 39;
  4
  5use operator_overload;
  6
  7# Workaround for 
  8#   ok( not (expression) , "test description" );
  9# does not working in older versions of Perl, eg 5.004_04
 10sub ok_not ($;$) {
 11    my($test, $name) = @_;
 12    $test = not $test;
 13    ok($test, $name);
 14}
 15
 16pass("loaded");
 17
 18# first check all the operators are implemented correctly from pure C++ code
 19operator_overloadc::Op_sanity_check();
 20
 21my $op = operator_overload::Op->new();
 22
 23isa_ok($op, "operator_overload::Op");
 24
 25my $op2 = operator_overload::Op->new();
 26
 27isa_ok($op2, "operator_overload::Op");
 28
 29# operator equal
 30$op->{i} = 5;
 31$op2->{i} = 3;
 32
 33ok_not(($op == $op2), "operator equal: not equal");
 34
 35$op->{i} = 3;
 36ok(($op == $op2), "operator equal: equal");
 37
 38# operator not equal
 39$op->{i} = 5;
 40$op2->{i} = 3;
 41
 42ok(($op != $op2), "operator not equal: not equal");
 43
 44$op->{i} = 3;
 45ok_not(($op != $op2), "operator not equal: equal");
 46
 47# stringify operator
 48$op->{i} = 3;
 49is("Op(3)", "$op", "operator stringify");
 50
 51# addition operator
 52$op->{i} = 3;
 53$op2->{i} = 3;
 54my $op3 = $op + $op2;
 55is($op3->{i}, 6, "operator addition");
 56
 57# addition assignment operator
 58$op->{i} = 3;
 59$op2->{i} = 3;
 60$op += $op2;
 61is($op->{i}, 6, "operator additive assignment");
 62
 63# subtraction operator
 64$op3->{i} = 6;
 65$op2->{i} = 3;
 66$op = $op3 - $op2;
 67is($op->{i}, 3, "operator subtraction");
 68
 69# reversed subtraction operator (with int)
 70$op3->{i} = 3;
 71$op = 6 - $op3;
 72is($op->{i}, 3, "reversed operator subtraction (with int)");
 73
 74# subtractive assignment operator
 75$op->{i} = 6;
 76$op2->{i} = 3;
 77$op -= $op2;
 78is($op->{i}, 3, "operator subtractive assignment");
 79
 80# multiplication operator
 81$op->{i} = 3;
 82$op2->{i} = 3;
 83$op3 = $op * $op2;
 84is($op3->{i}, 9, "operator multiplication");
 85
 86# division operator
 87$op->{i} = 9;
 88$op2->{i} = 3;
 89$op3 = $op / $op2;
 90is($op3->{i}, 3, "operator division");
 91
 92# modulus operator
 93$op->{i} = 8;
 94$op2->{i} = 3;
 95$op3 = $op % $op2;
 96is($op3->{i}, 2, "operator modulus");
 97
 98# greater than operator
 99$op->{i} = 8;
100$op2->{i} = 3;
101ok($op > $op2, "operator greater than");
102ok_not(($op2 > $op), "operator greater than");
103$op->{i} = 3;
104ok_not(($op2 > $op), "operator greater than");
105ok_not(($op > $op2), "operator greater than");
106
107# greater than or equal operator
108$op->{i} = 8;
109$op2->{i} = 3;
110ok($op >= $op2, "operator greater than or equal");
111ok_not(($op2 >= $op), "operator greater than or equal");
112$op->{i} = 3;
113ok(($op2 >= $op), "operator greater than or equal");
114ok(($op >= $op2), "operator greater than or equal");
115
116# lesser than operator
117$op2->{i} = 8;
118$op->{i} = 3;
119ok($op < $op2, "operator lesser than");
120ok_not(($op2 < $op), "operator lesser than");
121$op2->{i} = 3;
122ok_not(($op2 < $op), "operator lesser than");
123ok_not(($op < $op2), "operator lesser than");
124
125# less than or equal operator
126$op2->{i} = 8;
127$op->{i} = 3;
128ok($op <= $op2, "operator lesser than or equal");
129ok_not(($op2 <= $op), "operator lesser than or equal");
130$op2->{i} = 3;
131ok(($op2 <= $op), "operator less than or equal");
132ok(($op <= $op2), "operator less than or equal");
133
134# post-increment operator
135$op->{i} = 7;
136$op++;
137is($op->{i}, 8, "operator post-increment");
138
139# pre-increment operator
140$op->{i} = 7;
141++$op;
142is($op->{i}, 8, "operator pre-increment");
143
144# post-decrement operator
145$op->{i} = 7;
146$op--;
147is($op->{i}, 6, "operator post-decrement");
148
149# pre-decrement operator
150$op->{i} = 7;
151--$op;
152is($op->{i}, 6, "operator pre-decrement");
153
154# neg operator
155$op->{i} = 3;
156$op2 = -$op;
157is($op2->{i}, -3, "operator neg");
158
159# not operator
160$op->{i} = 0;
161is(!$op, !0, "operator not");
162
163$op->{i} = 1;
164is(!$op, !1, "operator not");
165
166### # and operator
167### $op->{i} = 4;
168### $op2->{i} = 2;
169### 
170### is($op & $op2, 4 & 2, "operator and");
171### 
172### isnt(($op & $op2), (10 & 2), "operator and - false");
173
174# fail("testing failed condition");
175