More stream tests

This commit is contained in:
bakaq
2025-01-31 13:14:45 -03:00
parent ad211d5e05
commit 28926486e0
4 changed files with 183 additions and 57 deletions

View File

@@ -6,11 +6,13 @@ use crate::heap_iter::{stackful_post_order_iter, NonListElider};
use crate::machine::machine_indices::VarKey;
use crate::machine::mock_wam::CompositeOpDir;
use crate::machine::{
F64Offset, F64Ptr, Fixnum, Number, BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS,
ArenaHeaderTag, F64Offset, F64Ptr, Fixnum, Number, BREAK_FROM_DISPATCH_LOOP_LOC,
LIB_QUERY_SUCCESS,
};
use crate::parser::ast::{Var, VarPtr};
use crate::parser::parser::{Parser, Tokens};
use crate::read::{write_term_to_heap, TermWriteResult};
use crate::types::UntypedArenaPtr;
use dashu::{Integer, Rational};
use indexmap::IndexMap;
@@ -280,11 +282,32 @@ impl Term {
(HeapCellValueTag::Fixnum, n) => {
term_stack.push(Term::Integer(n.into()));
}
(HeapCellValueTag::Cons) => {
match Number::try_from(addr) {
Ok(Number::Integer(i)) => term_stack.push(Term::Integer((*i).clone())),
Ok(Number::Rational(r)) => term_stack.push(Term::Rational((*r).clone())),
_ => {}
(HeapCellValueTag::Cons, ptr) => {
if let Ok(n) = Number::try_from(addr) {
match n {
Number::Integer(i) => term_stack.push(Term::Integer((*i).clone())),
Number::Rational(r) => term_stack.push(Term::Rational((*r).clone())),
_ => { unreachable!() },
}
} else {
match_untyped_arena_ptr!(ptr,
(ArenaHeaderTag::Stream, stream) => {
let stream_term = if let Some(alias) = stream.options().get_alias() {
Term::atom(alias.as_str().to_string())
} else {
Term::compound("$stream", [
Term::integer(stream.as_ptr() as usize)
])
};
term_stack.push(stream_term);
}
(ArenaHeaderTag::Dropped, _stream) => {
term_stack.push(Term::atom("$dropped_value"));
}
_ => {
unreachable!();
}
);
}
}
(HeapCellValueTag::CStr, s) => {
@@ -394,6 +417,7 @@ impl Term {
}
*/
_ => {
unreachable!();
}
);
}

View File

@@ -1,8 +1,5 @@
use std::io::Write;
use std::{cell::RefCell, io::Read, rc::Rc};
use super::*;
use crate::{MachineBuilder, StreamConfig};
use crate::MachineBuilder;
#[test]
#[cfg_attr(miri, ignore = "it takes too long to run")]
@@ -611,34 +608,3 @@ fn errors_and_exceptions() {
[Ok(LeafAnswer::Exception(Term::atom("a")))]
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn callback_streams() {
let test_string = Rc::new(RefCell::new(String::new()));
let test_string2 = test_string.clone();
let (mut user_input, streams) = StreamConfig::with_callbacks(
Some(Box::new(move |x| {
x.read_to_string(&mut test_string2.borrow_mut()).unwrap();
})),
None,
);
let mut machine = MachineBuilder::default().with_streams(streams).build();
write!(&mut user_input, "a(1,2,3).").unwrap();
let complete_answer: Vec<_> = machine
.run_query("read(A), write('asdf'), nl, flush_output.")
.collect();
assert_eq!(
complete_answer,
[Ok(LeafAnswer::from_bindings([(
"A",
Term::compound("a", [Term::integer(1), Term::integer(2), Term::integer(3)])
),]))]
);
assert_eq!(*test_string.borrow(), "asdf\n");
}