|
2 | 2 |
|
3 | 3 | import org.jamplate.glucose.spec.tool.DebugSpec; |
4 | 4 | import org.jamplate.impl.document.PseudoDocument; |
| 5 | +import org.jamplate.impl.unit.Action; |
5 | 6 | import org.jamplate.impl.unit.UnitImpl; |
6 | 7 | import org.jamplate.model.Document; |
7 | 8 | import org.jamplate.unit.Unit; |
8 | 9 | import org.junit.jupiter.api.Test; |
9 | 10 |
|
| 11 | +import java.io.ByteArrayOutputStream; |
| 12 | +import java.io.OutputStream; |
| 13 | +import java.io.PrintStream; |
| 14 | + |
| 15 | +import static org.jamplate.glucose.internal.util.Values.number; |
| 16 | +import static org.jamplate.util.Specs.listener; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
10 | 18 | import static org.junit.jupiter.api.Assertions.fail; |
11 | 19 |
|
12 | 20 | public class ExamplesTest { |
13 | 21 | @Test |
14 | 22 | public void _99bottlesOfBeerOnTheWall() { |
| 23 | + //manual test :) |
15 | 24 | Document document = new PseudoDocument( |
16 | 25 | "Test", |
17 | 26 | "#declare $i 99", |
@@ -55,4 +64,119 @@ public void _99bottlesOfBeerOnTheWall() { |
55 | 64 | fail("Test not completed"); |
56 | 65 | } |
57 | 66 | } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void _fibonacci() { |
| 70 | + for ( |
| 71 | + int[] arr : new int[][]{ |
| 72 | + {0, 0}, |
| 73 | + {1, 1}, |
| 74 | + {2, 1}, |
| 75 | + {3, 2}, |
| 76 | + {4, 3}, |
| 77 | + {5, 5}, |
| 78 | + {6, 8}, |
| 79 | + {7, 13}, |
| 80 | + {8, 21}, |
| 81 | + {9, 34}, |
| 82 | + {10, 55} |
| 83 | + } |
| 84 | + ) { |
| 85 | + Document fibonacci = new PseudoDocument( |
| 86 | + "fibonacci.jh", |
| 87 | + " #if $p == 0", |
| 88 | + " #declare $r 0", |
| 89 | + " #elif $p == 1", |
| 90 | + " #declare $r 1", |
| 91 | + " #else", |
| 92 | + " #for $ort [$rt]", |
| 93 | + " #declare $p $p - 1", |
| 94 | + " #include __PATH__", |
| 95 | + " #declare $rt $r", |
| 96 | + "", |
| 97 | + " #declare $p $p - 1", |
| 98 | + " #include __PATH__", |
| 99 | + " #declare $rt $rt + $r", |
| 100 | + "", |
| 101 | + " #declare $p $p + 2", |
| 102 | + " #declare $r $rt", |
| 103 | + " #declare $rt $ort", |
| 104 | + " #endfor", |
| 105 | + " #endif" |
| 106 | + ); |
| 107 | + Document main = new PseudoDocument( |
| 108 | + "main.jamplate", |
| 109 | + " #declare $p $input", |
| 110 | + " #include __DIR__ 'fibonacci.jh'", |
| 111 | + " #message $r" |
| 112 | + ); |
| 113 | + |
| 114 | + Unit unit = new UnitImpl(new GlucoseSpec()); |
| 115 | + |
| 116 | + unit.getSpec().add(listener(event -> { |
| 117 | + if (event.getAction().equals(Action.PRE_EXEC)) |
| 118 | + event.getMemory().set( |
| 119 | + "$input", |
| 120 | + number(arr[0]) |
| 121 | + ); |
| 122 | + })); |
| 123 | + |
| 124 | + OutputStream outBuffer = new ByteArrayOutputStream(); |
| 125 | + PrintStream out = System.out; |
| 126 | + System.setOut(new PrintStream(outBuffer)); |
| 127 | + |
| 128 | + if ( |
| 129 | + !unit.initialize(fibonacci, main) || |
| 130 | + !unit.parse(fibonacci, main) || |
| 131 | + !unit.analyze(fibonacci, main) || |
| 132 | + !unit.compile(fibonacci, main) || |
| 133 | + !unit.execute(main) |
| 134 | + ) { |
| 135 | + unit.diagnostic(); |
| 136 | + fail("Test not completed"); |
| 137 | + } |
| 138 | + |
| 139 | + assertEquals( |
| 140 | + Integer.toString(arr[1]), |
| 141 | + outBuffer.toString(), |
| 142 | + "Fibonacci seq" |
| 143 | + ); |
| 144 | + System.setOut(out); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void _quine() { |
| 150 | + Document document = new PseudoDocument( |
| 151 | + //name |
| 152 | + "Main", |
| 153 | + //content |
| 154 | + "#declare input '#message \"#declare input \" \"\\'\" input \"\\'\" \"\\n\" input'", |
| 155 | + "#message \"#declare input \" \"\\'\" input \"\\'\" \"\\n\" input" |
| 156 | + ); |
| 157 | + |
| 158 | + Unit unit = new UnitImpl(new GlucoseSpec()); |
| 159 | + |
| 160 | + OutputStream outBuffer = new ByteArrayOutputStream(); |
| 161 | + PrintStream out = System.out; |
| 162 | + System.setOut(new PrintStream(outBuffer)); |
| 163 | + |
| 164 | + if ( |
| 165 | + !unit.initialize(document) || |
| 166 | + !unit.parse(document) || |
| 167 | + !unit.analyze(document) || |
| 168 | + !unit.compile(document) || |
| 169 | + !unit.execute(document) |
| 170 | + ) { |
| 171 | + unit.diagnostic(); |
| 172 | + fail("Test not completed"); |
| 173 | + } |
| 174 | + |
| 175 | + assertEquals( |
| 176 | + document.read(), |
| 177 | + outBuffer.toString(), |
| 178 | + "Weird Quine" |
| 179 | + ); |
| 180 | + System.setOut(out); |
| 181 | + } |
58 | 182 | } |
0 commit comments