changeset 695:f5b34a1c2707

ecl_vm: add a test for Call and Return.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 02:30:57 +0200
parents 3ff1af76e413
children 7ae576a418ff
files src/th06/ecl_vm.rs
diffstat 1 files changed, 47 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/th06/ecl_vm.rs
+++ b/src/th06/ecl_vm.rs
@@ -956,3 +956,50 @@ impl EclRunner {
         }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::th06::anm0::Anm0;
+    use crate::th06::ecl::{Sub, CallSub, Rank};
+    use crate::th06::enemy::{Game, Position};
+    use std::io::{self, Read};
+    use std::fs::File;
+
+    fn setup() -> (Rc<RefCell<Game>>, Rc<RefCell<Enemy>>) {
+        let file = File::open("EoSD/ST/stg1enm.anm").unwrap();
+        let mut file = io::BufReader::new(file);
+        let mut buf = vec![];
+        file.read_to_end(&mut buf).unwrap();
+        let anm0 = Anm0::from_slice(&buf).unwrap();
+        let anm0 = Rc::new(RefCell::new(anm0));
+        let prng = Rc::new(RefCell::new(Prng::new(0)));
+        let game = Game::new(prng, Rank::Easy);
+        let game = Rc::new(RefCell::new(game));
+        let enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, Rc::downgrade(&anm0), Rc::downgrade(&game));
+        (game, enemy)
+    }
+
+    #[test]
+    fn call_and_return() {
+        let (game, enemy) = setup();
+        let ecl = Ecl { mains: vec![], subs: vec![
+            Sub { instructions: vec![
+                CallSub::new(0, Rank::Easy, SubInstruction::Call(1, 13, 12.)),
+            ]},
+            Sub { instructions: vec![
+                CallSub::new(0, Rank::Easy, SubInstruction::Noop()),
+                CallSub::new(1, Rank::Easy, SubInstruction::Return()),
+            ]},
+        ]};
+        let mut ecl_runner = EclRunner::new(&ecl, enemy, 0);
+        ecl_runner.run_frame();
+        assert_eq!(ecl_runner.frame.ints1[0], 13);
+        assert_eq!(ecl_runner.frame.floats[0], 12.);
+        assert_eq!(ecl_runner.stack.len(), 1);
+        ecl_runner.run_frame();
+        assert_eq!(ecl_runner.frame.ints1[0], 0);
+        assert_eq!(ecl_runner.frame.floats[0], 0.);
+        assert_eq!(ecl_runner.stack.len(), 0);
+    }
+}