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