about summary refs log tree commit diff stats
path: root/forth/foreforthfourth/test-advanced.js
diff options
context:
space:
mode:
Diffstat (limited to 'forth/foreforthfourth/test-advanced.js')
-rw-r--r--forth/foreforthfourth/test-advanced.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/forth/foreforthfourth/test-advanced.js b/forth/foreforthfourth/test-advanced.js
new file mode 100644
index 0000000..330fd81
--- /dev/null
+++ b/forth/foreforthfourth/test-advanced.js
@@ -0,0 +1,94 @@
+// Advanced test file for string operations and control flow
+// Run with: node test-advanced.js
+
+const ForthInterpreter = require('./forth.js');
+
+console.log('🧪 Testing Advanced 4-Stack Forth Features\n');
+
+// Test 1: String literals
+console.log('Test 1: String literals');
+let state = ForthInterpreter.createInitialState();
+state = ForthInterpreter.parseAndExecute(state, '." Hello World"');
+console.log('Stack 1 after string literal:', state.stacks[0]);
+console.log('Expected: ["Hello World"]\n');
+
+// Test 2: String operations
+console.log('Test 2: String operations');
+state = ForthInterpreter.parseAndExecute(state, 'dup strlen');
+console.log('Stack 1 after strlen:', state.stacks[0]);
+console.log('Expected: ["Hello World", 11]\n');
+
+// Test 3: String concatenation
+console.log('Test 3: String concatenation');
+state = ForthInterpreter.parseAndExecute(state, '."  from Forth" strcat');
+console.log('Stack 1 after strcat:', state.stacks[0]);
+console.log('Expected: ["Hello World from Forth"]\n');
+
+// Test 4: Basic control flow - IF THEN
+console.log('Test 4: Basic control flow - IF THEN');
+state = ForthInterpreter.parseAndExecute(state, '5 3 > if ." Greater" then');
+console.log('Output:', state.output[state.output.length - 1]);
+console.log('Expected: "Greater"\n');
+
+// Test 5: Control flow with false condition
+console.log('Test 5: Control flow with false condition');
+state = ForthInterpreter.parseAndExecute(state, '3 5 > if ." Greater" then');
+console.log('Output:', state.output[state.output.length - 1]);
+console.log('Expected: No output (condition was false)\n');
+
+// Test 6: IF ELSE THEN
+console.log('Test 6: IF ELSE THEN');
+state = ForthInterpreter.parseAndExecute(state, '5 3 > if ." Greater" else ." Less or Equal" then');
+console.log('Output:', state.output[state.output.length - 1]);
+console.log('Expected: "Greater"\n');
+
+// Test 7: IF ELSE THEN with false condition
+console.log('Test 7: IF ELSE THEN with false condition');
+state = ForthInterpreter.parseAndExecute(state, '3 5 > if ." Greater" else ." Less or Equal" then');
+console.log('Output:', state.output[state.output.length - 1]);
+console.log('Expected: "Less or Equal"\n');
+
+// Test 8: BEGIN UNTIL loop
+console.log('Test 8: BEGIN UNTIL loop');
+state = ForthInterpreter.parseAndExecute(state, '5 begin dup ." Loop " 1 - dup 0 = until drop');
+console.log('Output:', state.output.slice(-5));
+console.log('Expected: 5 loop iterations\n');
+
+// Test 9: Complex control flow
+console.log('Test 9: Complex control flow');
+state = ForthInterpreter.parseAndExecute(state, '10 begin dup 0 > if dup ." Count: " . 1 - else drop 0 then dup 0 = until');
+console.log('Output:', state.output.slice(-10));
+console.log('Expected: Countdown from 10 to 1\n');
+
+// Test 10: String manipulation with control flow
+console.log('Test 10: String manipulation with control flow');
+state = ForthInterpreter.parseAndExecute(state, '." Test" dup strlen 5 > if ." Long string" else ." Short string" then');
+console.log('Output:', state.output.slice(-3));
+console.log('Expected: "Test", "Short string"\n');
+
+// Test 11: Nested control flow
+console.log('Test 11: Nested control flow');
+state = ForthInterpreter.parseAndExecute(state, '7 dup 5 > if dup 10 > if ." Very large" else ." Large" then else dup 3 > if ." Medium" else ." Small" then then');
+console.log('Output:', state.output[state.output.length - 1]);
+console.log('Expected: "Large"\n');
+
+// Test 12: String operations with numbers
+console.log('Test 12: String operations with numbers');
+state = ForthInterpreter.parseAndExecute(state, '." Hello" 32 char+');
+console.log('Stack 1 after char+:', state.stacks[0]);
+console.log('Expected: ["Hello "]\n');
+
+console.log('✅ All advanced tests completed!');
+console.log('\nFinal state:');
+console.log('Stack 1:', state.stacks[0]);
+console.log('Stack 2:', state.stacks[1]);
+console.log('Stack 3:', state.stacks[2]);
+console.log('Stack 4:', state.stacks[3]);
+console.log('Dictionary size:', state.dictionary.size);
+console.log('Output messages:', state.output.length);
+
+// Show some example outputs
+console.log('\nSample outputs:');
+state.output.slice(-5).forEach((msg, i) => {
+    console.log(`${i + 1}. ${msg}`);
+});