11// quickchpl: Algebraic Properties Example
2- // Demonstrates testing algebraic laws using the Patterns library
2+ // Demonstrates testing algebraic laws using the Patterns predicates
33
44module AlgebraicProperties {
55 use quickchpl;
@@ -14,23 +14,25 @@ module AlgebraicProperties {
1414 writeln (" -" * 40 );
1515
1616 {
17- var gen = intGen(- 100 , 100 );
17+ var gen = tupleGen( intGen(- 100 , 100 ), intGen( - 100 , 100 ) );
1818
1919 // Commutativity: a + b = b + a
20- var commProp = commutativeProperty (" integer addition" , gen,
21- proc (a : int , b : int ) { return a + b ; });
20+ var commProp = property (" integer addition is commutative " , gen,
21+ proc (args : ( int , int )) { const (a, b) = args; return intAddCommutative(a, b) ; });
2222 var result1 = check(commProp);
2323 printResult(result1.passed, commProp.name, result1.numTests);
2424
2525 // Associativity: (a + b) + c = a + (b + c)
26- var assocProp = associativeProperty(" integer addition" , gen,
27- proc (a: int , b: int ) { return a + b; });
26+ var gen3 = tupleGen(intGen(- 100 , 100 ), intGen(- 100 , 100 ), intGen(- 100 , 100 ));
27+ var assocProp = property(" integer addition is associative" , gen3,
28+ proc (args: (int , int , int )) { const (a, b, c) = args; return intAddAssociative(a, b, c); });
2829 var result2 = check(assocProp);
2930 printResult(result2.passed, assocProp.name, result2.numTests);
3031
3132 // Identity: a + 0 = a
32- var idProp = identityProperty(" integer addition" , gen,
33- proc (a: int , b: int ) { return a + b; }, 0 );
33+ var gen1 = intGen(- 100 , 100 );
34+ var idProp = property(" zero is additive identity" , gen1,
35+ proc (a: int ) { return intAddIdentity(a); });
3436 var result3 = check(idProp);
3537 printResult(result3.passed, idProp.name, result3.numTests);
3638 }
@@ -41,23 +43,25 @@ module AlgebraicProperties {
4143 writeln (" -" * 40 );
4244
4345 {
44- var gen = intGen(- 10 , 10 ); // Smaller range to avoid overflow
46+ var gen = tupleGen( intGen(- 10 , 10 ), intGen( - 10 , 10 ));
4547
4648 // Commutativity: a * b = b * a
47- var commProp = commutativeProperty (" integer multiplication" , gen,
48- proc (a : int , b : int ) { return a * b ; });
49+ var commProp = property (" integer multiplication is commutative " , gen,
50+ proc (args : ( int , int )) { const (a, b) = args; return intMulCommutative(a, b) ; });
4951 var result1 = check(commProp);
5052 printResult(result1.passed, commProp.name, result1.numTests);
5153
5254 // Associativity: (a * b) * c = a * (b * c)
53- var assocProp = associativeProperty(" integer multiplication" , gen,
54- proc (a: int , b: int ) { return a * b; });
55+ var gen3 = tupleGen(intGen(- 10 , 10 ), intGen(- 10 , 10 ), intGen(- 10 , 10 ));
56+ var assocProp = property(" integer multiplication is associative" , gen3,
57+ proc (args: (int , int , int )) { const (a, b, c) = args; return intMulAssociative(a, b, c); });
5558 var result2 = check(assocProp);
5659 printResult(result2.passed, assocProp.name, result2.numTests);
5760
5861 // Identity: a * 1 = a
59- var idProp = identityProperty(" integer multiplication" , gen,
60- proc (a: int , b: int ) { return a * b; }, 1 );
62+ var gen1 = intGen(- 10 , 10 );
63+ var idProp = property(" one is multiplicative identity" , gen1,
64+ proc (a: int ) { return intMulIdentity(a); });
6165 var result3 = check(idProp);
6266 printResult(result3.passed, idProp.name, result3.numTests);
6367 }
@@ -68,11 +72,10 @@ module AlgebraicProperties {
6872 writeln (" -" * 40 );
6973
7074 {
71- var gen = intGen(- 10 , 10 );
75+ var gen3 = tupleGen( intGen(- 10 , 10 ), intGen( - 10 , 10 ), intGen( - 10 , 10 ) );
7276
73- var distProp = distributiveProperty(" multiplication over addition" , gen,
74- proc (a: int , b: int ) { return a * b; },
75- proc (a: int , b: int ) { return a + b; });
77+ var distProp = property(" multiplication distributes over addition" , gen3,
78+ proc (args: (int , int , int )) { const (a, b, c) = args; return intDistributive(a, b, c); });
7679 var result = check(distProp);
7780 printResult(result.passed, distProp.name, result.numTests);
7881 }
@@ -83,29 +86,31 @@ module AlgebraicProperties {
8386 writeln (" -" * 40 );
8487
8588 {
86- var gen = intGen(- 100 , 100 );
89+ var gen = tupleGen( intGen(- 100 , 100 ), intGen( - 100 , 100 ) );
8790
8891 // max is commutative
89- var maxCommProp = commutativeProperty (" max" , gen,
90- proc (a : int , b : int ) { return max (a, b); });
92+ var maxCommProp = property (" max is commutative " , gen,
93+ proc (args : ( int , int )) { const (a, b) = args; return maxCommutative (a, b); });
9194 var result1 = check(maxCommProp);
9295 printResult(result1.passed, maxCommProp.name, result1.numTests);
9396
9497 // max is associative
95- var maxAssocProp = associativeProperty(" max" , gen,
96- proc (a: int , b: int ) { return max (a, b); });
98+ var gen3 = tupleGen(intGen(- 100 , 100 ), intGen(- 100 , 100 ), intGen(- 100 , 100 ));
99+ var maxAssocProp = property(" max is associative" , gen3,
100+ proc (args: (int , int , int )) { const (a, b, c) = args; return maxAssociative(a, b, c); });
97101 var result2 = check(maxAssocProp);
98102 printResult(result2.passed, maxAssocProp.name, result2.numTests);
99103
100104 // min is commutative
101- var minCommProp = commutativeProperty (" min" , gen,
102- proc (a : int , b : int ) { return min (a, b); });
105+ var minCommProp = property (" min is commutative " , gen,
106+ proc (args : ( int , int )) { const (a, b) = args; return minCommutative (a, b); });
103107 var result3 = check(minCommProp);
104108 printResult(result3.passed, minCommProp.name, result3.numTests);
105109
106110 // Idempotence: max(a, a) = a
107- var idempProp = idempotentProperty(" max(x, x)" , gen,
108- proc (x: int ) { return max (x, x); });
111+ var gen1 = intGen(- 100 , 100 );
112+ var idempProp = property(" max is idempotent" , gen1,
113+ proc (a: int ) { return maxIdempotent(a); });
109114 var result4 = check(idempProp);
110115 printResult(result4.passed, idempProp.name, result4.numTests);
111116 }
0 commit comments