|
| 1 | +---------------------------- MODULE TestApalache ----------------------------- |
| 2 | +(* |
| 3 | + * Test the module Apalache against TLC. |
| 4 | + * |
| 5 | + * Igor Konnov, 2026 |
| 6 | + *) |
| 7 | + |
| 8 | +EXTENDS Sequences, Integers, Apalache |
| 9 | + |
| 10 | +VARIABLE |
| 11 | + \* This is needed for TLC to work |
| 12 | + \* @type: Int; |
| 13 | + xxx |
| 14 | + |
| 15 | +Init == xxx = 0 |
| 16 | +Next == UNCHANGED xxx |
| 17 | + |
| 18 | +(********************* DEFINITIONS ********************************) |
| 19 | +\* @type: Seq(Int); |
| 20 | +seq345 == <<3, 4, 5>> |
| 21 | + |
| 22 | +\* @type: Seq(Int); |
| 23 | +seqEmpty == <<>> |
| 24 | + |
| 25 | +\* @type: Seq(Int); |
| 26 | +seq26970 == <<2, 6, 9, 7, 0>> |
| 27 | + |
| 28 | +Add(i, j) == i + j |
| 29 | + |
| 30 | +IsOdd(i) == i % 2 = 1 |
| 31 | + |
| 32 | +Concat(s, t) == s \o t |
| 33 | + |
| 34 | +\* The set { 1, 2, 3, 5, 6, 7 } |
| 35 | +\* that has duplicates in its internal representation |
| 36 | +Set123567 == |
| 37 | + LET one == 1 IN |
| 38 | + LET two == 2 IN |
| 39 | + LET three == one + two IN |
| 40 | + \* 3 and three are equal but the set constructor will miss that |
| 41 | + { one, two, 3, three, three, 5, 6, 7, 7 } |
| 42 | + |
| 43 | +\* The set { 1, 3, 5, 7 } |
| 44 | +\* that has ghost cells 2, 6 in its internal representation |
| 45 | +Set1357 == |
| 46 | + { i \in Set123567: i % 2 /= 0 } |
| 47 | + |
| 48 | +\* The set { 2, 6 } |
| 49 | +\* that has ghost cells 1, 3, three, 5 in its internal representation |
| 50 | +Set26 == |
| 51 | + { i \in Set123567: i % 2 = 0 } |
| 52 | + |
| 53 | +\* The set { 2, 3, 6 } |
| 54 | +\* that has ghost cells 1, 3, three, 5 and non-ghost cells 2, 3, 6 |
| 55 | +Set263 == |
| 56 | + Set26 \union { 3 } |
| 57 | + |
| 58 | +SetEmpty == |
| 59 | + \* create an empty set yet avoiding constant propagation |
| 60 | + LET x == 1 IN |
| 61 | + { 1 } \ { x } |
| 62 | + |
| 63 | +(********************* TESTS ********************************) |
| 64 | + |
| 65 | +TestFoldSeq26970 == |
| 66 | + ApaFoldSeqLeft(Add, 0, seq26970) = 2 + 6 + 9 + 7 |
| 67 | + |
| 68 | +TestFoldSeq697 == |
| 69 | + ApaFoldSeqLeft(Add, 0, SubSeq(seq26970, 2, 4)) = 6 + 9 + 7 |
| 70 | + |
| 71 | +TestFoldSeq345 == |
| 72 | + ApaFoldSeqLeft(Add, 0, Tail(seq345)) = 4 + 5 |
| 73 | + |
| 74 | +TestFoldSeqEmpty == |
| 75 | + ApaFoldSeqLeft(Add, 0, seqEmpty) = 0 |
| 76 | + |
| 77 | +TestFoldSeqFlatten == |
| 78 | + ApaFoldSeqLeft(Concat, <<>>, <<seq345, seqEmpty, seq26970>>) = <<3, 4, 5, 2, 6, 9, 7, 0>> |
| 79 | + |
| 80 | +TestMkSeqDouble == |
| 81 | + LET Double(i) == 2 * i IN |
| 82 | + MkSeq(4, Double) = <<2, 4, 6, 8>> |
| 83 | + |
| 84 | +TestMkSeqEmpty == |
| 85 | + LET Double(i) == 2 * i IN |
| 86 | + MkSeq(0, Double) = << >> |
| 87 | + |
| 88 | +TestMkSeqConcat == |
| 89 | + LET Double(i) == 2 * i IN |
| 90 | + LET Triple(i) == 3 * i IN |
| 91 | + MkSeq(2, Double) \o MkSeq(3, Triple) = <<2, 4, 3, 6, 9>> |
| 92 | + |
| 93 | +TestMkSeqSubSeq == |
| 94 | + LET Triple(i) == 3 * i IN |
| 95 | + SubSeq(MkSeq(5, Triple), 2, 3) = <<6, 9>> |
| 96 | + |
| 97 | +TestMkSeqLen == |
| 98 | + LET Double(i) == 2 * i IN |
| 99 | + Len(MkSeq(4, Double)) = 4 |
| 100 | + |
| 101 | +TestMkSeqFold == |
| 102 | + LET Double(i) == 2 * i IN |
| 103 | + ApaFoldSeqLeft(Add, 0, MkSeq(4, Double)) = 20 |
| 104 | + |
| 105 | +TestFoldSeqOverSelectSeq == |
| 106 | + ApaFoldSeqLeft(Add, 0, SelectSeq(seq345, IsOdd)) = 3 + 5 |
| 107 | + |
| 108 | +TestFunAsSeq3 == |
| 109 | + LET f == [ i \in 1..3 |-> i + 1 ] IN |
| 110 | + FunAsSeq(f, 3, 3) = <<2, 3, 4>> |
| 111 | + |
| 112 | +TestFunAsSeqEmpty == |
| 113 | + LET |
| 114 | + \* @type: Int -> Int; |
| 115 | + f == [ i \in {} |-> i ] |
| 116 | + IN |
| 117 | + FunAsSeq(f, 0, 0) = << >> |
| 118 | + |
| 119 | +\* TLC fails here, so we do not include this test |
| 120 | +TestFunAsSeqLargerCapacity == |
| 121 | + LET f == [ i \in 1..3 |-> i + 1 ] IN |
| 122 | + \* provide a larger upper bound |
| 123 | + FunAsSeq(f, 3, 10) = <<2, 3, 4>> |
| 124 | + |
| 125 | +TestGuessSet == |
| 126 | + LET S == |
| 127 | + Guess({ T \in { SetEmpty, Set263, Set1357 }: 6 \in T }) |
| 128 | + IN |
| 129 | + S = Set263 |
| 130 | + |
| 131 | +TestGuessSetFromTwo == |
| 132 | + LET S == |
| 133 | + Guess({ T \in { SetEmpty, Set263, Set1357 }: 3 \in T }) |
| 134 | + IN |
| 135 | + S \in { Set263, Set1357 } |
| 136 | + |
| 137 | +TestGuessInSingleton == |
| 138 | + LET x == Guess({ 3 }) IN |
| 139 | + x = 3 |
| 140 | + |
| 141 | +TestGuessEmpty == |
| 142 | + LET x == Guess(SetEmpty) IN |
| 143 | + \* This expression is equivalent to TRUE. |
| 144 | + \* We are using to avoid constant simplification of TRUE. |
| 145 | + x <= 0 \/ x > 0 |
| 146 | + |
| 147 | +TestApaFoldSet == |
| 148 | + LET \* @type: (Set(Int), Int) => Set(Int); |
| 149 | + A(p,q) == p \union {q} |
| 150 | + IN |
| 151 | + ApaFoldSet( A, {4,4}, {1,1,2,2,3,3} ) = {1,2,3,4} |
| 152 | + |
| 153 | +\* this test is a disjunction of all smaller tests |
| 154 | +AllTests == |
| 155 | + /\ TestFoldSeq26970 |
| 156 | + /\ TestFoldSeq697 |
| 157 | + /\ TestFoldSeq345 |
| 158 | + /\ TestFoldSeqEmpty |
| 159 | + /\ TestFoldSeqFlatten |
| 160 | + /\ TestFoldSeqOverSelectSeq |
| 161 | + /\ TestMkSeqDouble |
| 162 | + /\ TestMkSeqEmpty |
| 163 | + /\ TestMkSeqConcat |
| 164 | + /\ TestMkSeqSubSeq |
| 165 | + /\ TestMkSeqLen |
| 166 | + /\ TestMkSeqFold |
| 167 | + /\ TestFunAsSeq3 |
| 168 | + /\ TestFunAsSeqEmpty |
| 169 | + /\ TestGuessSet |
| 170 | + /\ TestGuessSetFromTwo |
| 171 | + /\ TestGuessInSingleton |
| 172 | + /\ TestApaFoldSet |
| 173 | + \* TLC fails here |
| 174 | + \*/\ TestGuessEmpty |
| 175 | + \*/\ TestFunAsSeqLargerCapacity |
| 176 | + |
| 177 | +=============================================================================== |
0 commit comments