diff options
| author | Brian Cully <bjc@spork.org> | 2025-08-24 18:25:10 -0400 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-08-24 21:56:16 -0400 |
| commit | 0f9a9158b7135a3162522cf473a7bad80f141109 (patch) | |
| tree | 241523674d811fa9c300e0b449de19232267184e | |
| parent | 9539017ec481653dc47e3e362ae6d240aad1b917 (diff) | |
| download | automathon-0f9a9158b7135a3162522cf473a7bad80f141109.tar.gz automathon-0f9a9158b7135a3162522cf473a7bad80f141109.zip | |
move factorial test to integration test
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/forth/mod.rs | 33 | ||||
| -rw-r--r-- | tests/forth.rs | 28 |
3 files changed, 29 insertions, 34 deletions
@@ -5,7 +5,7 @@ edition = "2024" license-file = "LICENSE" [lib] -crate-type = ["cdylib"] +crate-type = ["cdylib", "rlib"] [dependencies] console_log = "1.0" diff --git a/src/forth/mod.rs b/src/forth/mod.rs index f61bbda..aea8bda 100644 --- a/src/forth/mod.rs +++ b/src/forth/mod.rs @@ -1,35 +1,2 @@ pub mod compiler; pub mod vm; - -#[cfg(test)] -mod tests { - use super::{ - vm::{VM, WordList}, - compiler::Compiler, - }; - - fn compiler_for(text: &str) -> Compiler { - let mut p = Compiler::new(text); - p.compile().expect("badparse"); - p - } - - fn eprintwordlist(wordlist: &WordList) { - for i in 0..wordlist.0.len() { - eprintln!("wordlist[{}]: {:?}", i, wordlist.0[i]); - } - } - - // recursive! - #[test] - fn fac() { - let prog = ": fac dup 1 > if dup 1 - fac * then ; 5 fac\n"; - let p = compiler_for(prog); - eprintwordlist(&p.wordlist); - let mut vm = VM::new(p.wordlist); - vm.run().expect("should run to completion"); - eprintln!("stack: {:?}", vm.stack); - assert_eq!(vm.stack.0.len(), 1, "factorial result stack len"); - assert_eq!(vm.stack.0[0], 120, "factorial result value"); - } -} diff --git a/tests/forth.rs b/tests/forth.rs new file mode 100644 index 0000000..4fae28e --- /dev/null +++ b/tests/forth.rs @@ -0,0 +1,28 @@ +use automathon::forth::{ + compiler::Compiler, + vm::{WordList, VM}, +}; + +fn compiler_for(text: &str) -> Compiler { + let mut p = Compiler::new(text); + p.compile().expect("badparse"); + p +} + +fn eprintwordlist(wordlist: &WordList) { + for i in 0..wordlist.0.len() { + eprintln!("wordlist[{}]: {:?}", i, wordlist.0[i]); + } +} + +#[test] +fn factorial() { + let prog = ": fac dup 1 > if dup 1 - fac * then ; 5 fac\n"; + let comp = compiler_for(prog); + eprintwordlist(&comp.wordlist); + let mut vm = VM::new(comp.wordlist); + vm.run().expect("should run to completion"); + eprintln!("stack: {:?}", vm.stack); + assert_eq!(vm.stack.0.len(), 1, "factorial result stack len"); + assert_eq!(vm.stack.0[0], 120, "factorial result value"); +} |
