1 | namespace HeuristicLab.Tests.Benchmark {
|
---|
2 |
|
---|
3 | using HeuristicLab.BenchmarkSuite;
|
---|
4 | using HeuristicLab.BenchmarkSuite.Problems;
|
---|
5 | using HeuristicLab.Random;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis;
|
---|
7 |
|
---|
8 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
9 |
|
---|
10 | [TestClass]
|
---|
11 | public class SolutionTests {
|
---|
12 | private void TestProgram(PushProgram program, IBenchmarkSuiteDataDescriptor descriptor) {
|
---|
13 | var data = descriptor.CreateProblemData();
|
---|
14 | var config = new PushConfiguration {
|
---|
15 | EvalPushLimit = data.EvalLimit,
|
---|
16 | MaxProgramLength = data.MaxSize,
|
---|
17 | ErcOptions = data.ErcOptions,
|
---|
18 | FloatStringFormat = data.FloatStringFormat
|
---|
19 | };
|
---|
20 |
|
---|
21 | config.SetEnabledStacks((StackTypes)data.EnabledDataTypes);
|
---|
22 | config.InitInExpressions(data.TotalInputArgumentCount);
|
---|
23 |
|
---|
24 | var evaluator = new PushBenchmarkSuiteEvaluator(data);
|
---|
25 | var pool = new PushInterpreterPool(config);
|
---|
26 |
|
---|
27 | var trainingResult = evaluator.EvaluateTraining(pool, program, new MersenneTwister());
|
---|
28 | var testResult = evaluator.EvaluateTest(pool, program, new MersenneTwister());
|
---|
29 |
|
---|
30 | Assert.AreEqual(0, trainingResult.AvgQuality);
|
---|
31 | Assert.AreEqual(0, testResult.AvgQuality);
|
---|
32 | }
|
---|
33 |
|
---|
34 | [TestMethod]
|
---|
35 | [TestProperty("Time", "Short")]
|
---|
36 | [TestCategory("ProblemTest")]
|
---|
37 | public void Checksum() {
|
---|
38 | TestProgram(
|
---|
39 | PushParser.ParseProgram(
|
---|
40 | @"(
|
---|
41 | 0 STRING.ITERATE (
|
---|
42 | INTEGER.FROMCHAR INTEGER.+
|
---|
43 | )
|
---|
44 | 64 INTEGER.% ' ' INTEGER.FROMCHAR INTEGER.+
|
---|
45 | ""Check sum is "" PRINT.PRINTSTRING
|
---|
46 | CHAR.FROMINTEGER PRINT.PRINTCHAR
|
---|
47 | )"),
|
---|
48 | new Checksum());
|
---|
49 | }
|
---|
50 |
|
---|
51 | [TestMethod]
|
---|
52 | [TestProperty("Time", "Short")]
|
---|
53 | [TestCategory("ProblemTest")]
|
---|
54 | public void CollatzNumbers() {
|
---|
55 | TestProgram(
|
---|
56 | PushParser.ParseProgram(
|
---|
57 | @"(
|
---|
58 | INTEGER.DUP 1 INTEGER.= BOOLEAN.NOT
|
---|
59 | EXEC.WHILE (
|
---|
60 | INTEGER.DUP INTEGER.DUP 2 INTEGER.% 0 INTEGER.=
|
---|
61 | EXEC.IF (
|
---|
62 | 2 INTEGER./
|
---|
63 | ) (
|
---|
64 | 3 INTEGER.* 1 INTEGER.+
|
---|
65 | )
|
---|
66 | INTEGER.DUP 1 INTEGER.= BOOLEAN.NOT
|
---|
67 | )
|
---|
68 | INTEGER.POP INTEGER.STACKDEPTH
|
---|
69 | )"),
|
---|
70 | new CollatzNumbers());
|
---|
71 | }
|
---|
72 |
|
---|
73 | [TestMethod]
|
---|
74 | [TestProperty("Time", "Short")]
|
---|
75 | [TestCategory("ProblemTest")]
|
---|
76 | public void CompareStringLengths() {
|
---|
77 | TestProgram(
|
---|
78 | PushParser.ParseProgram(@"
|
---|
79 | (
|
---|
80 | STRING.LENGTH STRING.LENGTH INTEGER.DUP
|
---|
81 | STRING.LENGTH INTEGER.> INTEGER.> BOOLEAN.AND
|
---|
82 | )"),
|
---|
83 | new CompareStringLengths());
|
---|
84 | }
|
---|
85 |
|
---|
86 | [TestMethod]
|
---|
87 | [TestProperty("Time", "Short")]
|
---|
88 | [TestCategory("ProblemTest")]
|
---|
89 | public void CountOdds() {
|
---|
90 | TestProgram(
|
---|
91 | PushParser.ParseProgram(@"
|
---|
92 | (
|
---|
93 | 0 INTEGER[].ITERATE (
|
---|
94 | 2 INTEGER.% INTEGER.DUP
|
---|
95 | 1 INTEGER.=
|
---|
96 | -1 INTEGER.=
|
---|
97 | BOOLEAN.OR
|
---|
98 | EXEC.WHEN INTEGER.INC
|
---|
99 | )
|
---|
100 | )"),
|
---|
101 | new CountOdds());
|
---|
102 | }
|
---|
103 |
|
---|
104 | [TestMethod]
|
---|
105 | [TestProperty("Time", "Short")]
|
---|
106 | [TestCategory("ProblemTest")]
|
---|
107 | public void Digits() {
|
---|
108 | TestProgram(
|
---|
109 | PushParser.ParseProgram(@"
|
---|
110 | (
|
---|
111 | INTEGER.DUP STRING.FROMINTEGER
|
---|
112 | INTEGER.DUP 0 INTEGER.< BOOLEAN.DUP
|
---|
113 | BOOLEAN.DUP INTEGER.DUP -10 INTEGER.> BOOLEAN.AND
|
---|
114 | EXEC.WHEN (
|
---|
115 | PRINT.PRINTSTRING
|
---|
116 | EXEC.FLUSH
|
---|
117 | )
|
---|
118 | EXEC.WHEN (
|
---|
119 | STRING.REST STRING.DUP STRING.FIRST STRING.SWAP STRING.REST
|
---|
120 | )
|
---|
121 | STRING.REVERSE
|
---|
122 | STRING.DUP STRING.FIRST PRINT.PRINTSTRING
|
---|
123 | STRING.REST STRING.ITERATE (
|
---|
124 | PRINT.NEWLINE PRINT.PRINTCHAR
|
---|
125 | )
|
---|
126 | EXEC.WHEN (
|
---|
127 | PRINT.NEWLINE
|
---|
128 | '-' PRINT.PRINTCHAR PRINT.PRINTSTRING
|
---|
129 | )
|
---|
130 | )"),
|
---|
131 | new Digits());
|
---|
132 | }
|
---|
133 |
|
---|
134 | [TestMethod]
|
---|
135 | [TestProperty("Time", "Short")]
|
---|
136 | [TestCategory("ProblemTest")]
|
---|
137 | public void DoubleLetters() {
|
---|
138 | TestProgram(
|
---|
139 | PushParser.ParseProgram(@"
|
---|
140 | (
|
---|
141 | STRING.ITERATE (
|
---|
142 | 3 EXEC.DO*COUNT CHAR.DUP
|
---|
143 | CHAR.ISLETTER EXEC.WHEN PRINT.PRINTCHAR
|
---|
144 | '!' CHAR.= EXEC.WHEN (
|
---|
145 | CHAR.DUP PRINT.PRINTCHAR PRINT.PRINTCHAR
|
---|
146 | )
|
---|
147 | PRINT.PRINTCHAR
|
---|
148 | )
|
---|
149 | )"),
|
---|
150 | new DoubleLetters());
|
---|
151 | }
|
---|
152 |
|
---|
153 | [TestMethod]
|
---|
154 | [TestProperty("Time", "Short")]
|
---|
155 | [TestCategory("ProblemTest")]
|
---|
156 | public void EvenSquares() {
|
---|
157 | TestProgram(
|
---|
158 | PushParser.ParseProgram(@"
|
---|
159 | (
|
---|
160 | INTEGER.DUP EXEC.DO*COUNT (
|
---|
161 | INTEGER.DUP 0 INTEGER.>
|
---|
162 | EXEC.WHEN (
|
---|
163 | INTEGER.DUP 2 INTEGER.*
|
---|
164 | INTEGER.DUP INTEGER.DUP INTEGER.*
|
---|
165 | INTEGER.DUP 4 INTEGER.YANKDUP INTEGER.<
|
---|
166 | EXEC.IF (
|
---|
167 | PRINT.ENSURE_NEWLINE
|
---|
168 | PRINT.PRINTINTEGER
|
---|
169 | )
|
---|
170 | EXEC.FLUSH
|
---|
171 | INTEGER.POP
|
---|
172 | )
|
---|
173 | INTEGER.POP
|
---|
174 | )
|
---|
175 | )"),
|
---|
176 | new EvenSquares());
|
---|
177 | }
|
---|
178 |
|
---|
179 | [TestMethod]
|
---|
180 | [TestProperty("Time", "Short")]
|
---|
181 | [TestCategory("ProblemTest")]
|
---|
182 | public void ForLoopIndex() {
|
---|
183 | TestProgram(
|
---|
184 | PushParser.ParseProgram(@"
|
---|
185 | (
|
---|
186 | STRING.FROMINTEGER INTEGER.SWAP true
|
---|
187 | EXEC.WHILE (
|
---|
188 | INTEGER.DUP PRINT.ENSURE_NEWLINE PRINT.PRINTINTEGER
|
---|
189 | STRING.DUP INTEGER.FROMSTRING INTEGER.+
|
---|
190 | INTEGER.DUP 2 INTEGER.YANKDUP INTEGER.<
|
---|
191 | )
|
---|
192 | )"),
|
---|
193 | new ForLoopIndex());
|
---|
194 | }
|
---|
195 |
|
---|
196 | [TestMethod]
|
---|
197 | [TestProperty("Time", "Short")]
|
---|
198 | [TestCategory("ProblemTest")]
|
---|
199 | public void Grades() {
|
---|
200 | TestProgram(
|
---|
201 | PushParser.ParseProgram(@"
|
---|
202 | (
|
---|
203 | ""Student has a "" PRINT.PRINTSTRING
|
---|
204 | INTEGER.DUP INTEGER.ROT INTEGER.>
|
---|
205 | EXEC.WHEN ( ""F"" "" grade."" STRING.CONCAT PRINT.PRINTSTRING EXEC.FLUSH )
|
---|
206 | INTEGER.DUP INTEGER.ROT INTEGER.>
|
---|
207 | EXEC.WHEN ( ""D"" "" grade."" STRING.CONCAT PRINT.PRINTSTRING EXEC.FLUSH )
|
---|
208 | INTEGER.DUP INTEGER.ROT INTEGER.>
|
---|
209 | EXEC.WHEN ( ""C"" "" grade."" STRING.CONCAT PRINT.PRINTSTRING EXEC.FLUSH )
|
---|
210 | INTEGER.DUP INTEGER.ROT INTEGER.>
|
---|
211 | EXEC.WHEN ( ""B"" "" grade."" STRING.CONCAT PRINT.PRINTSTRING EXEC.FLUSH )
|
---|
212 | ""A"" "" grade."" STRING.CONCAT PRINT.PRINTSTRING
|
---|
213 | )"),
|
---|
214 | new Grades());
|
---|
215 | }
|
---|
216 |
|
---|
217 | [TestMethod]
|
---|
218 | [TestProperty("Time", "Short")]
|
---|
219 | [TestCategory("ProblemTest")]
|
---|
220 | public void LastIndexOfZero() {
|
---|
221 | TestProgram(
|
---|
222 | PushParser.ParseProgram(@"
|
---|
223 | (
|
---|
224 | INTEGER[].DUP INTEGER[].LENGTH INTEGER[].REVERSE
|
---|
225 | 0 INTEGER[].INDEXOF INTEGER.- INTEGER.DEC
|
---|
226 | )"),
|
---|
227 | new LastIndexOfZero());
|
---|
228 | }
|
---|
229 |
|
---|
230 | [TestMethod]
|
---|
231 | [TestProperty("Time", "Short")]
|
---|
232 | [TestCategory("ProblemTest")]
|
---|
233 | public void Median() {
|
---|
234 | TestProgram(
|
---|
235 | PushParser.ParseProgram(@"
|
---|
236 | (
|
---|
237 | INTEGER.ROT IN1 IN2 INTEGER.MAX
|
---|
238 | IN3 INTEGER.MIN
|
---|
239 | INTEGER.ROT INTEGER.MIN INTEGER.MAX
|
---|
240 | PRINT.PRINTINTEGER
|
---|
241 | )"),
|
---|
242 | new Median());
|
---|
243 | }
|
---|
244 |
|
---|
245 | [TestMethod]
|
---|
246 | [TestProperty("Time", "Short")]
|
---|
247 | [TestCategory("ProblemTest")]
|
---|
248 | public void MirrorImage() {
|
---|
249 | TestProgram(
|
---|
250 | PushParser.ParseProgram(@"
|
---|
251 | (
|
---|
252 | INTEGER[].PUSHALL INTEGER[].ITERATE (
|
---|
253 | INTEGER.= BOOLEAN.NOT EXEC.WHEN (
|
---|
254 | false EXEC.FLUSH
|
---|
255 | )
|
---|
256 | )
|
---|
257 | true
|
---|
258 | )"),
|
---|
259 | new MirrorImage());
|
---|
260 | }
|
---|
261 |
|
---|
262 | [TestMethod]
|
---|
263 | [TestProperty("Time", "Short")]
|
---|
264 | [TestCategory("ProblemTest")]
|
---|
265 | public void NegativeToZero() {
|
---|
266 | TestProgram(
|
---|
267 | PushParser.ParseProgram(@"
|
---|
268 | (
|
---|
269 | INTEGER[].DUP INTEGER[].LENGTH INTEGER.DEC
|
---|
270 | EXEC.DO*COUNT (
|
---|
271 | INTEGER.DUP INTEGER[].DUP INTEGER[].NTH 0 INTEGER.<
|
---|
272 | EXEC.WHEN (
|
---|
273 | 0 INTEGER[].SET
|
---|
274 | )
|
---|
275 | )
|
---|
276 | )"),
|
---|
277 | new NegativeToZero());
|
---|
278 | }
|
---|
279 |
|
---|
280 | [TestMethod]
|
---|
281 | [TestProperty("Time", "Short")]
|
---|
282 | [TestCategory("ProblemTest")]
|
---|
283 | public void NumberIo() {
|
---|
284 | TestProgram(
|
---|
285 | PushParser.ParseProgram(@"
|
---|
286 | (
|
---|
287 | FLOAT.FROMINTEGER FLOAT.+ PRINT.PRINTFLOAT
|
---|
288 | )"),
|
---|
289 | new NumberIO());
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | [TestMethod]
|
---|
294 | [TestProperty("Time", "Short")]
|
---|
295 | [TestCategory("ProblemTest")]
|
---|
296 | public void PigLatin() {
|
---|
297 | TestProgram(
|
---|
298 | PushParser.ParseProgram(@"
|
---|
299 | (
|
---|
300 | STRING.SPLIT STRING.STACKDEPTH 0 INTEGER.=
|
---|
301 | EXEC.WHEN EXEC.FLUSH
|
---|
302 | STRING.STACKDEPTH EXEC.DO*TIMES (
|
---|
303 | STRING.DUP STRING.FIRST CHAR.ALLFROMSTRING CHAR.DUP ""aeiou""
|
---|
304 | STRING.CONTAINSCHAR EXEC.IF (
|
---|
305 | CHAR.POP
|
---|
306 | ) (
|
---|
307 | STRING.REST STRING.CONJCHAR
|
---|
308 | )
|
---|
309 | ""ay"" STRING.CONCAT
|
---|
310 | STRING.STACKDEPTH 1 INTEGER.>
|
---|
311 | EXEC.WHEN (
|
---|
312 | ' ' STRING.CONJCHAR
|
---|
313 | )
|
---|
314 | PRINT.PRINTSTRING
|
---|
315 | )
|
---|
316 | )"),
|
---|
317 | new PigLatin());
|
---|
318 | }
|
---|
319 |
|
---|
320 | [TestMethod]
|
---|
321 | [TestProperty("Time", "Short")]
|
---|
322 | [TestCategory("ProblemTest")]
|
---|
323 | public void ReplaceSpaceWithNewLine() {
|
---|
324 |
|
---|
325 | TestProgram(
|
---|
326 | PushParser.ParseProgram(@"
|
---|
327 | (
|
---|
328 | STRING.DUP 0 STRING.ITERATE (
|
---|
329 | CHAR.DUP ' ' CHAR.=
|
---|
330 | EXEC.IF (
|
---|
331 | CHAR.POP '\n'
|
---|
332 | )
|
---|
333 | INTEGER.INC PRINT.PRINTCHAR
|
---|
334 | )
|
---|
335 | )"),
|
---|
336 | new ReplaceSpaceWithNewline());
|
---|
337 | }
|
---|
338 |
|
---|
339 | [TestMethod]
|
---|
340 | [TestProperty("Time", "Short")]
|
---|
341 | [TestCategory("ProblemTest")]
|
---|
342 | public void ScrabbleScore() {
|
---|
343 | TestProgram(
|
---|
344 | PushParser.ParseProgram(@"
|
---|
345 | (
|
---|
346 | STRING.ITERATE (
|
---|
347 | INTEGER.FROMCHAR INTEGER.DUP INTEGER.DUP
|
---|
348 | 96 INTEGER.>
|
---|
349 | 123 INTEGER.<
|
---|
350 | BOOLEAN.AND EXEC.WHEN (
|
---|
351 | 32 INTEGER.-
|
---|
352 | )
|
---|
353 | 65 INTEGER.- INTEGER.DUP INTEGER.DUP
|
---|
354 | 0 INTEGER.>=
|
---|
355 | 26 INTEGER.<
|
---|
356 | BOOLEAN.AND EXEC.IF (
|
---|
357 | [1 3 3 2 1 4 2 4 1 8 5 1 3 1 1 3 10 1 1 1 1 4 4 8 4 10]
|
---|
358 | INTEGER[].NTH INTEGER.+
|
---|
359 | ) (
|
---|
360 | INTEGER.POP
|
---|
361 | )
|
---|
362 | )
|
---|
363 | INTEGER.EMPTY EXEC.WHEN 0
|
---|
364 | )"),
|
---|
365 | new ScrabbleScore());
|
---|
366 | }
|
---|
367 |
|
---|
368 | [TestMethod]
|
---|
369 | [TestProperty("Time", "Short")]
|
---|
370 | [TestCategory("ProblemTest")]
|
---|
371 | public void Smallest() {
|
---|
372 | TestProgram(
|
---|
373 | PushParser.ParseProgram(@"
|
---|
374 | (
|
---|
375 | 3 EXEC.DO*TIMES INTEGER.MIN
|
---|
376 | PRINT.PRINTINTEGER
|
---|
377 | )"),
|
---|
378 | new Smallest());
|
---|
379 | }
|
---|
380 |
|
---|
381 | [TestMethod]
|
---|
382 | [TestProperty("Time", "Short")]
|
---|
383 | [TestCategory("ProblemTest")]
|
---|
384 | public void SmallOrLarge() {
|
---|
385 | TestProgram(
|
---|
386 | PushParser.ParseProgram(@"
|
---|
387 | (
|
---|
388 | INTEGER.DUP 1000 INTEGER.< 2000 INTEGER.>=
|
---|
389 | EXEC.WHEN ""large""
|
---|
390 | EXEC.WHEN ""small""
|
---|
391 | STRING.EMPTY
|
---|
392 | EXEC.WHEN """"
|
---|
393 | PRINT.PRINTSTRING
|
---|
394 | )"),
|
---|
395 | new SmallOrLarge());
|
---|
396 | }
|
---|
397 |
|
---|
398 | [TestMethod]
|
---|
399 | [TestProperty("Time", "Short")]
|
---|
400 | [TestCategory("ProblemTest")]
|
---|
401 | public void StringDifferences() {
|
---|
402 | TestProgram(
|
---|
403 | PushParser.ParseProgram(@"
|
---|
404 | (
|
---|
405 | CHAR.ALLFROMSTRING 0
|
---|
406 | STRING.ITERATE (
|
---|
407 | CHAR.DUP 2 CHAR.YANKDUP CHAR.= BOOLEAN.NOT EXEC.IF (
|
---|
408 | INTEGER.DUP PRINT.ENSURE_NEWLINE PRINT.PRINTINTEGER
|
---|
409 | ' ' PRINT.PRINTCHAR PRINT.PRINTCHAR
|
---|
410 | ' ' PRINT.PRINTCHAR PRINT.PRINTCHAR
|
---|
411 | ) (
|
---|
412 | CHAR.POP CHAR.POP
|
---|
413 | )
|
---|
414 | INTEGER.INC
|
---|
415 | )
|
---|
416 | )"),
|
---|
417 | new StringDifferences());
|
---|
418 | }
|
---|
419 |
|
---|
420 | [TestMethod]
|
---|
421 | [TestProperty("Time", "Short")]
|
---|
422 | [TestCategory("ProblemTest")]
|
---|
423 | public void StringLengthsBackwards() {
|
---|
424 | TestProgram(
|
---|
425 | PushParser.ParseProgram(@"
|
---|
426 | (
|
---|
427 | STRING[].ITERATE (
|
---|
428 | STRING.LENGTH PRINT.ENSURE_NEWLINE PRINT.PRINTINTEGER
|
---|
429 | )
|
---|
430 | )"),
|
---|
431 | new StringLengthsBackwards());
|
---|
432 | }
|
---|
433 |
|
---|
434 | [TestMethod]
|
---|
435 | [TestProperty("Time", "Short")]
|
---|
436 | [TestCategory("ProblemTest")]
|
---|
437 | public void SumOfSquares() {
|
---|
438 | TestProgram(
|
---|
439 | PushParser.ParseProgram(@"
|
---|
440 | (
|
---|
441 | EXEC.DO*COUNT (
|
---|
442 | INTEGER.DUP INTEGER.* INTEGER.+
|
---|
443 | )
|
---|
444 | )"),
|
---|
445 | new SumOfSquares());
|
---|
446 | }
|
---|
447 |
|
---|
448 | [TestMethod]
|
---|
449 | [TestProperty("Time", "Short")]
|
---|
450 | [TestCategory("ProblemTest")]
|
---|
451 | public void SuperAnagrams() {
|
---|
452 | TestProgram(
|
---|
453 | PushParser.ParseProgram(@"
|
---|
454 | (
|
---|
455 | STRING.SWAP STRING.DUP STRING.ITERATE (
|
---|
456 | CHAR.DUP STRING.DUP STRING.OCCURRENCESOFCHAR
|
---|
457 | 1 STRING.YANKDUP STRING.OCCURRENCESOFCHAR
|
---|
458 | INTEGER.> EXEC.WHEN (
|
---|
459 | false EXEC.FLUSH
|
---|
460 | )
|
---|
461 | )
|
---|
462 | true
|
---|
463 | )"),
|
---|
464 | new SuperAnagrams());
|
---|
465 | }
|
---|
466 |
|
---|
467 | [TestMethod]
|
---|
468 | [TestProperty("Time", "Short")]
|
---|
469 | [TestCategory("ProblemTest")]
|
---|
470 | public void Syllables() {
|
---|
471 | TestProgram(
|
---|
472 | PushParser.ParseProgram(@"
|
---|
473 | (
|
---|
474 | ""aeiouy"" 0 STRING.ITERATE (
|
---|
475 | STRING.DUP STRING.OCCURRENCESOFCHAR INTEGER.+
|
---|
476 | )
|
---|
477 | ""The number of syllables is ""
|
---|
478 | PRINT.PRINTSTRING PRINT.PRINTINTEGER
|
---|
479 | )"),
|
---|
480 | new Syllables());
|
---|
481 | }
|
---|
482 |
|
---|
483 | [TestMethod]
|
---|
484 | [TestProperty("Time", "Short")]
|
---|
485 | [TestCategory("ProblemTest")]
|
---|
486 | public void VectorAverage() {
|
---|
487 | TestProgram(
|
---|
488 | PushParser.ParseProgram(@"
|
---|
489 | (
|
---|
490 | FLOAT[].DUP FLOAT[].LENGTH
|
---|
491 | FLOAT[].ITERATE FLOAT.+
|
---|
492 | FLOAT.FROMINTEGER FLOAT./
|
---|
493 | )"),
|
---|
494 | new VectorAverage());
|
---|
495 | }
|
---|
496 |
|
---|
497 | [TestMethod]
|
---|
498 | [TestProperty("Time", "Short")]
|
---|
499 | [TestCategory("ProblemTest")]
|
---|
500 | public void VectorSummed() {
|
---|
501 | TestProgram(
|
---|
502 | PushParser.ParseProgram(@"
|
---|
503 | (
|
---|
504 | INTEGER[].PUSHALL INTEGER[].DUP
|
---|
505 | INTEGER[].LENGTH INTEGER.DEC
|
---|
506 | EXEC.DO*COUNT (
|
---|
507 | INTEGER.DUP INTEGER[].DUP INTEGER[].NTH
|
---|
508 | INTEGER.SWAP INTEGER.ROT INTEGER.+ INTEGER[].SET
|
---|
509 | )
|
---|
510 | )"),
|
---|
511 | new VectorSummed());
|
---|
512 | }
|
---|
513 |
|
---|
514 | [TestMethod]
|
---|
515 | [TestProperty("Time", "Short")]
|
---|
516 | [TestCategory("ProblemTest")]
|
---|
517 | public void WallisPi() {
|
---|
518 | TestProgram(
|
---|
519 | PushParser.ParseProgram(@"
|
---|
520 | (
|
---|
521 | 2 3 INTEGER.ROT INTEGER.ROT
|
---|
522 | EXEC.DO*TIMES (
|
---|
523 | 2 EXEC.DO*TIMES (
|
---|
524 | 1 INTEGER.YANKDUP 1 INTEGER.YANKDUP
|
---|
525 | )
|
---|
526 | FLOAT.FROMINTEGER FLOAT.FROMINTEGER FLOAT.SWAP FLOAT./ FLOAT.* INTEGER.<
|
---|
527 | EXEC.IF (
|
---|
528 | INTEGER.SWAP 2 INTEGER.+ INTEGER.SWAP
|
---|
529 | ) (
|
---|
530 | 2 INTEGER.+
|
---|
531 | )
|
---|
532 | )
|
---|
533 | )"),
|
---|
534 | new WallisPi());
|
---|
535 | }
|
---|
536 |
|
---|
537 | [TestMethod]
|
---|
538 | [TestProperty("Time", "Short")]
|
---|
539 | [TestCategory("ProblemTest")]
|
---|
540 | public void WordStats() {
|
---|
541 | TestProgram(
|
---|
542 | PushParser.ParseProgram(@"
|
---|
543 | (
|
---|
544 | STRING.DUP STRING.SPLIT
|
---|
545 | STRING.STACKDEPTH 1 INTEGER.= BOOLEAN.NOT
|
---|
546 | EXEC.WHEN (
|
---|
547 | true EXEC.WHILE (
|
---|
548 | STRING.LENGTH INTEGER.MAX STRING.STACKDEPTH 1 INTEGER.= BOOLEAN.NOT
|
---|
549 | )
|
---|
550 | [0] 2 INTEGER.- INTEGER.DUP 0 INTEGER.>=
|
---|
551 | EXEC.WHEN (
|
---|
552 | INTEGER.INC EXEC.DO*TIMES (
|
---|
553 | 0 INTEGER[].CONJ
|
---|
554 | )
|
---|
555 | )
|
---|
556 | STRING.DUP STRING.SPLIT STRING.STACKDEPTH 1 INTEGER.>
|
---|
557 | EXEC.WHILE (
|
---|
558 | STRING.LENGTH INTEGER.DEC INTEGER.DUP INTEGER[].DUP
|
---|
559 | INTEGER[].NTH INTEGER.INC INTEGER[].SET STRING.STACKDEPTH 1 INTEGER.>
|
---|
560 | )
|
---|
561 | INTEGER[].DUP INTEGER[].LENGTH INTEGER.DEC
|
---|
562 | EXEC.DO*COUNT (
|
---|
563 | PRINT.ENSURE_NEWLINE
|
---|
564 | ""words of length "" PRINT.PRINTSTRING
|
---|
565 | INTEGER.DUP INTEGER.INC PRINT.PRINTINTEGER
|
---|
566 | "": "" PRINT.PRINTSTRING
|
---|
567 | INTEGER[].DUP INTEGER[].NTH PRINT.PRINTINTEGER
|
---|
568 | )
|
---|
569 | '.' STRING.DUP STRING.OCCURRENCESOFCHAR
|
---|
570 | '!' STRING.DUP STRING.OCCURRENCESOFCHAR INTEGER.+
|
---|
571 | '?' STRING.DUP STRING.OCCURRENCESOFCHAR INTEGER.+
|
---|
572 | INTEGER.DUP PRINT.ENSURE_NEWLINE
|
---|
573 | ""number of sentences: "" PRINT.PRINTSTRING PRINT.PRINTINTEGER
|
---|
574 | STRING.SPLIT STRING.STACKDEPTH FLOAT.FROMINTEGER FLOAT.FROMINTEGER FLOAT./
|
---|
575 | PRINT.ENSURE_NEWLINE
|
---|
576 | ""average sentence length: "" PRINT.PRINTSTRING PRINT.PRINTFLOAT
|
---|
577 | )
|
---|
578 | )"),
|
---|
579 | new WordStats());
|
---|
580 | }
|
---|
581 |
|
---|
582 | [TestMethod]
|
---|
583 | [TestProperty("Time", "Short")]
|
---|
584 | [TestCategory("ProblemTest")]
|
---|
585 | public void XWordLines() {
|
---|
586 | TestProgram(
|
---|
587 | PushParser.ParseProgram(@"
|
---|
588 | (
|
---|
589 | STRING.SPLIT STRING.EMPTY BOOLEAN.NOT INTEGER.DUP 1 INTEGER.=
|
---|
590 | EXEC.IF (
|
---|
591 | EXEC.WHILE (
|
---|
592 | PRINT.ENSURE_NEWLINE PRINT.PRINTSTRING
|
---|
593 | STRING.EMPTY BOOLEAN.NOT
|
---|
594 | )
|
---|
595 | ) (
|
---|
596 | 2 INTEGER.- EXEC.WHILE (
|
---|
597 | STRING.STACKDEPTH 2 INTEGER.-
|
---|
598 | 1 INTEGER.YANKDUP INTEGER.MIN
|
---|
599 | INTEGER.DUP 0 INTEGER.>=
|
---|
600 | EXEC.IF (
|
---|
601 | INTEGER.INC EXEC.DO*TIMES (
|
---|
602 | ' ' STRING.CONJCHAR STRING.SWAP STRING.CONCAT
|
---|
603 | )
|
---|
604 | PRINT.ENSURE_NEWLINE PRINT.PRINTSTRING
|
---|
605 | STRING.EMPTY BOOLEAN.NOT
|
---|
606 | ) (
|
---|
607 | PRINT.ENSURE_NEWLINE PRINT.PRINTSTRING
|
---|
608 | STRING.EMPTY BOOLEAN.NOT
|
---|
609 | )
|
---|
610 | )
|
---|
611 | )
|
---|
612 | )"),
|
---|
613 | new XWordLines());
|
---|
614 | }
|
---|
615 | }
|
---|
616 | }
|
---|