### RFC: Tutorial on Testing

by tmoertel (Chaplain)
 on Sep 14, 2004 at 04:32 UTC Need Help??

```# File: AngularDifference.pm

...
}

1;
```
```# File: AngularDifference.t

...
# multiples of 360-degrees shouldn't matter
my (\$a,\$b) = (360 * 2, 360 * 4);
is( angdiff(\$a,\$b+23),23, "\$a,\$b+23 -> 23" );  # case 6
```
```\$ perl AngularDifference.t
1..6
...
#     expected: '90'
ok 6 - 720,1440+23 -> 23
# Looks like you failed 1 tests of 6.
```
```sub angdiff(\$\$) {
my (\$a, \$b) = @_;
return abs(\$a - \$b) % 180;
}
```
```\$ perl AngularDifference.t
1..6
...
ok 4 - 45,0 -> 45
ok 5 - 0,270 -> 90, not 270
ok 6 - 720,1440+23 -> 23
```
```is( angdiff( 90, 90),   0, "zero at 90" );   # case 2
```
```# File: AngularDifference.l.t

...
##[ a <- Int, diff <- Int(range=>[-180,180]) ]##
angdiff(\$a, \$a + \$diff) == abs(\$diff)
}, name => "angdiff holds to defn of angular difference";
```
```    ##[ a <- Int, diff <- Int(range=>[-180,180]) ]##
```
```    angdiff(\$a, \$a + \$diff) == abs(\$diff)
```
```\$ perl AngularDifference.l.t
1..1
...
# Counterexample:
# \$a = 148;
# \$diff = 180;
```
```When \$a = 0

...
190           170             10  <-- Oops
200           160             20  <-- Oops
210           150             30  <-- Oops
```
```sub angdiff(\$\$) {
my (\$a, \$b) = @_;
my \$delta = (\$a - \$b) % 360;
return \$delta > 180 ? 360 - \$delta : \$delta;
}
```
```\$ perl AngularDifference.l.t
1..1
ok 1 - 'angdiff holds to defn of angular difference' (1000 attempts)
```
```Property {

...
angdiff(\$a, \$a + \$diff) == abs(\$diff)

}, name => "angdiff holds to defn of angular difference";
```
```1..1
ok 1 - 'angdiff holds to defn of angular difference' (1000 attempts)
#  58% 1 to 90
#  41% 91 to 180
#   0% zero
```
```Property {

...
angdiff(\$a, \$b) == abs(\$diff)

}, name => "angdiff holds to defn of angular difference";
```
```1..1
ok 1 - 'angdiff holds to defn of angular difference' (1000 attempts)
#  98% > 360
#   0% 181 to 360
#   0% 1 to 90
```
```n <- OneOf( Int(range=>[-1,1]), Int )
```
```Property {

...
angdiff(\$a, \$b) == abs(\$diff)

}, name => "angdiff holds to defn of angular difference";
```
```1..1
ok 1 - 'angdiff holds to defn of angular difference' (1000 attempts)
...
#   9% 1 to 90
#   6% 91 to 180
#   0% zero
```
```n <- Frequency( [20,Int(range=>[-1,1])], [1,Int] )
```
```1..1
ok 1 - 'angdiff holds to defn of angular difference' (1000 attempts)
...
#  18% 1 to 90
#  10% 91 to 180
#   0% zero
```

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://390737]
Approved by ysth
Front-paged by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2021-07-28 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?