|
| 1 | +#include <Misra/Generics.h> |
| 2 | +#include <Misra/Std/Io.h> |
| 3 | +#include <Misra/Types.h> |
| 4 | + |
| 5 | +#include "../Util/TestRunner.h" |
| 6 | + |
| 7 | +// Self-contained variants -- no global registration; two variants sharing a |
| 8 | +// payload type do not collide. |
| 9 | +Variant(Number, int, double); |
| 10 | +Variant(Cell, int, char); |
| 11 | +Variant(Tri, int, float, char); |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// Variant construction + Match / When (variant arms) / Otherwise |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | + |
| 17 | +bool test_variant_construct_and_match(void) { |
| 18 | + Number n = Number_from_int(42); |
| 19 | + bool hit = false; |
| 20 | + Match(n) { |
| 21 | + When(Number, int) hit = (it == 42); |
| 22 | + When(Number, double) hit = false; |
| 23 | + } |
| 24 | + |
| 25 | + Number m = Number_from_double(2.5); |
| 26 | + bool hd = false; |
| 27 | + Match(m) { |
| 28 | + When(Number, int) hd = false; |
| 29 | + When(Number, double) hd = (it == 2.5); |
| 30 | + } |
| 31 | + return hit && hd; |
| 32 | +} |
| 33 | + |
| 34 | +bool test_variant_it_is_typed_payload(void) { |
| 35 | + Number a = Number_from_int(10); |
| 36 | + Number b = Number_from_double(1.5); |
| 37 | + int ai = 0; |
| 38 | + double bd = 0.0; |
| 39 | + Match(a) { |
| 40 | + When(Number, int) ai = it * 2; // it : int |
| 41 | + When(Number, double) bd = it; |
| 42 | + } |
| 43 | + Match(b) { |
| 44 | + When(Number, int) ai = it; |
| 45 | + When(Number, double) bd = it * 2.0; // it : double |
| 46 | + } |
| 47 | + return ai == 20 && bd == 3.0; |
| 48 | +} |
| 49 | + |
| 50 | +// value in, value out -- no pointers |
| 51 | +static Number twice(Number n) { |
| 52 | + Match(n) { |
| 53 | + When(Number, int) return Number_from_int(it * 2); |
| 54 | + When(Number, double) return Number_from_double(it * 2.0); |
| 55 | + } |
| 56 | + return n; |
| 57 | +} |
| 58 | + |
| 59 | +bool test_variant_by_value_flow(void) { |
| 60 | + Number r = twice(Number_from_int(21)); |
| 61 | + bool ok = false; |
| 62 | + Match(r) { |
| 63 | + When(Number, int) ok = (it == 42); |
| 64 | + When(Number, double) ok = false; |
| 65 | + } |
| 66 | + return ok; |
| 67 | +} |
| 68 | + |
| 69 | +bool test_variant_rvalue_and_otherwise(void) { |
| 70 | + bool ok = false; |
| 71 | + // Match directly on a function-return rvalue; float-less Number -> Otherwise unused. |
| 72 | + Match(twice(Number_from_double(1.5))) { |
| 73 | + When(Number, int) ok = false; |
| 74 | + When(Number, double) ok = (it == 3.0); |
| 75 | + Otherwise ok = false; |
| 76 | + } |
| 77 | + return ok; |
| 78 | +} |
| 79 | + |
| 80 | +bool test_two_variants_independent(void) { |
| 81 | + Cell c = Cell_from_char('Z'); |
| 82 | + bool ok = false; |
| 83 | + Match(c) { |
| 84 | + When(Cell, int) ok = false; |
| 85 | + When(Cell, char) ok = (it == 'Z'); |
| 86 | + } |
| 87 | + return ok; |
| 88 | +} |
| 89 | + |
| 90 | +bool test_variant_three_payloads(void) { |
| 91 | + Tri vals[] = {Tri_from_int(7), Tri_from_float(1.5f), Tri_from_char('A')}; |
| 92 | + int seen = 0; |
| 93 | + bool ok = true; |
| 94 | + for (int k = 0; k < 3; k++) { |
| 95 | + Match(vals[k]) { |
| 96 | + When(Tri, int) ok = ok && (it == 7), seen |= 1; |
| 97 | + When(Tri, float) ok = ok && (it == 1.5f), seen |= 2; |
| 98 | + When(Tri, char) ok = ok && (it == 'A'), seen |= 4; |
| 99 | + } |
| 100 | + } |
| 101 | + return ok && seen == 7; |
| 102 | +} |
| 103 | + |
| 104 | +bool test_variant_arms_are_exclusive(void) { |
| 105 | + Number n = Number_from_int(7); |
| 106 | + int count = 0; |
| 107 | + Match(n) { |
| 108 | + When(Number, int) count++; |
| 109 | + When(Number, double) count++; |
| 110 | + Otherwise count++; |
| 111 | + } |
| 112 | + return count == 1; |
| 113 | +} |
| 114 | + |
| 115 | +// --------------------------------------------------------------------------- |
| 116 | +// Deadend: a non-exhaustive variant match aborts rather than fall through. |
| 117 | +// --------------------------------------------------------------------------- |
| 118 | + |
| 119 | +bool deadend_variant_nonexhaustive(void) { |
| 120 | + Number n = Number_from_double(9.0); // holds double, only int handled, no Otherwise |
| 121 | + Match(n) { |
| 122 | + When(Number, int)(void) it; |
| 123 | + } |
| 124 | + return true; // unreachable -- the match aborts first |
| 125 | +} |
| 126 | + |
| 127 | +int main(void) { |
| 128 | + WriteFmt("[INFO] Starting Generics.Variant tests\n\n"); |
| 129 | + TestFunction tests[] = { |
| 130 | + test_variant_construct_and_match, |
| 131 | + test_variant_it_is_typed_payload, |
| 132 | + test_variant_by_value_flow, |
| 133 | + test_variant_rvalue_and_otherwise, |
| 134 | + test_two_variants_independent, |
| 135 | + test_variant_three_payloads, |
| 136 | + test_variant_arms_are_exclusive, |
| 137 | + }; |
| 138 | + TestFunction deadend_tests[] = { |
| 139 | + deadend_variant_nonexhaustive, |
| 140 | + }; |
| 141 | + int total = sizeof(tests) / sizeof(tests[0]); |
| 142 | + int deadend = sizeof(deadend_tests) / sizeof(deadend_tests[0]); |
| 143 | + return run_test_suite(tests, total, deadend_tests, deadend, "Generics.Variant"); |
| 144 | +} |
0 commit comments