2024-03-15 18:54:25 +01:00
|
|
|
|
|
|
|
|
|
use {
|
|
|
|
|
r3vi::{
|
|
|
|
|
buffer::singleton::{
|
|
|
|
|
SingletonBuffer
|
|
|
|
|
},
|
|
|
|
|
view::port::UpdateTask
|
|
|
|
|
},
|
|
|
|
|
crate::{
|
|
|
|
|
repr_tree::{Context, ReprTreeExt, ReprTree, ReprLeaf}
|
|
|
|
|
},
|
|
|
|
|
std::sync::{Arc, RwLock}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn char_view() {
|
|
|
|
|
let ctx = Arc::new(RwLock::new(Context::new()));
|
|
|
|
|
crate::editors::digit::init_ctx( ctx.clone() );
|
|
|
|
|
|
|
|
|
|
let mut rt_digit = ReprTree::new_arc( Context::parse(&ctx, "<Digit 16>") );
|
|
|
|
|
rt_digit.insert_leaf(
|
|
|
|
|
Context::parse(&ctx, "Char"),
|
|
|
|
|
ReprLeaf::from_singleton_buffer( SingletonBuffer::new('5') )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//<><><><>
|
|
|
|
|
let mut digit_char_buffer = rt_digit
|
|
|
|
|
.descend( Context::parse(&ctx, "Char") ).unwrap()
|
|
|
|
|
.singleton_buffer::<char>();
|
|
|
|
|
|
|
|
|
|
assert_eq!( digit_char_buffer.get(), '5' );
|
|
|
|
|
//<><><><>
|
|
|
|
|
|
|
|
|
|
let digit_char_view = rt_digit
|
|
|
|
|
.descend(Context::parse(&ctx, "Char")).unwrap()
|
|
|
|
|
.view_char();
|
|
|
|
|
|
|
|
|
|
assert_eq!( digit_char_view.get_view().unwrap().get(), '5' );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//<><><><>
|
|
|
|
|
// `Char-view` is correctly coupled to `char-buffer`
|
|
|
|
|
digit_char_buffer.set('2');
|
|
|
|
|
assert_eq!( digit_char_view.get_view().unwrap().get(), '2' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2024-05-25 19:21:16 +02:00
|
|
|
|
fn digit_projection_char_to_u64() {
|
2024-03-15 18:54:25 +01:00
|
|
|
|
let ctx = Arc::new(RwLock::new(Context::new()));
|
|
|
|
|
crate::editors::digit::init_ctx( ctx.clone() );
|
|
|
|
|
|
|
|
|
|
let mut rt_digit = ReprTree::new_arc( Context::parse(&ctx, "<Digit 16>") );
|
|
|
|
|
|
|
|
|
|
rt_digit.insert_leaf(
|
|
|
|
|
Context::parse(&ctx, "Char"),
|
|
|
|
|
ReprLeaf::from_singleton_buffer( SingletonBuffer::new('5') )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//<><><><>
|
|
|
|
|
// add another representation
|
|
|
|
|
|
|
|
|
|
ctx.read().unwrap().morphisms.apply_morphism(
|
|
|
|
|
rt_digit.clone(),
|
|
|
|
|
&Context::parse(&ctx, "<Digit 16>~Char"),
|
2024-05-25 19:21:16 +02:00
|
|
|
|
&Context::parse(&ctx, "<Digit 16>~ℤ_2^64~machine.UInt64")
|
2024-03-15 18:54:25 +01:00
|
|
|
|
);
|
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
let digit_u64_view = rt_digit
|
|
|
|
|
.descend(Context::parse(&ctx, "ℤ_2^64~machine.UInt64")).unwrap()
|
|
|
|
|
.view_u64();
|
2024-03-15 18:54:25 +01:00
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
assert_eq!( digit_u64_view.get_view().unwrap().get(), 5 as u64 );
|
2024-03-15 18:54:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// projection behaves accordingly , when buffer is changed
|
|
|
|
|
|
|
|
|
|
let mut digit_char_buffer = rt_digit
|
|
|
|
|
.descend( Context::parse(&ctx, "Char") ).unwrap()
|
|
|
|
|
.singleton_buffer::<char>();
|
|
|
|
|
|
|
|
|
|
digit_char_buffer.set('2');
|
2024-05-25 19:21:16 +02:00
|
|
|
|
assert_eq!( digit_u64_view.get_view().unwrap().get(), 2 as u64 );
|
2024-03-15 18:54:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2024-05-25 19:21:16 +02:00
|
|
|
|
fn digit_projection_u64_to_char() {
|
2024-03-15 18:54:25 +01:00
|
|
|
|
let ctx = Arc::new(RwLock::new(Context::new()));
|
|
|
|
|
crate::editors::digit::init_ctx( ctx.clone() );
|
|
|
|
|
|
|
|
|
|
let mut rt_digit = ReprTree::new_arc( Context::parse(&ctx, "<Digit 16>") );
|
|
|
|
|
|
|
|
|
|
rt_digit.insert_leaf(
|
2024-05-25 19:21:16 +02:00
|
|
|
|
Context::parse(&ctx, "ℤ_2^64~machine.UInt64"),
|
|
|
|
|
ReprLeaf::from_singleton_buffer( SingletonBuffer::new(5 as u64) )
|
2024-03-15 18:54:25 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//<><><><>
|
|
|
|
|
// add another representation
|
|
|
|
|
|
|
|
|
|
ctx.read().unwrap().morphisms.apply_morphism(
|
|
|
|
|
rt_digit.clone(),
|
2024-05-25 19:21:16 +02:00
|
|
|
|
&Context::parse(&ctx, "<Digit 16>~ℤ_2^64~machine.UInt64"),
|
2024-03-15 18:54:25 +01:00
|
|
|
|
&Context::parse(&ctx, "<Digit 16>~Char")
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
let digit_u64_view = rt_digit
|
2024-03-15 18:54:25 +01:00
|
|
|
|
.descend(Context::parse(&ctx, "Char")).unwrap()
|
|
|
|
|
.view_char();
|
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
assert_eq!( digit_u64_view.get_view().unwrap().get(), '5' );
|
2024-03-15 18:54:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn char_buffered_projection() {
|
|
|
|
|
let ctx = Arc::new(RwLock::new(Context::new()));
|
|
|
|
|
crate::editors::digit::init_ctx( ctx.clone() );
|
|
|
|
|
|
|
|
|
|
let mut rt_digit = ReprTree::new_arc( Context::parse(&ctx, "<Digit 16>") );
|
|
|
|
|
|
|
|
|
|
rt_digit.insert_leaf(
|
2024-05-25 19:21:16 +02:00
|
|
|
|
Context::parse(&ctx, "ℤ_2^64~machine.UInt64"),
|
|
|
|
|
ReprLeaf::from_singleton_buffer( SingletonBuffer::new(8 as u64) )
|
2024-03-15 18:54:25 +01:00
|
|
|
|
);
|
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
let mut digit_u64_buffer = rt_digit
|
|
|
|
|
.descend(Context::parse(&ctx, "ℤ_2^64~machine.UInt64")).unwrap()
|
|
|
|
|
.singleton_buffer::<u64>();
|
2024-03-15 18:54:25 +01:00
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
assert_eq!( digit_u64_buffer.get(), 8 );
|
2024-03-15 18:54:25 +01:00
|
|
|
|
|
|
|
|
|
rt_digit.insert_leaf(
|
|
|
|
|
Context::parse(&ctx, "Char"),
|
|
|
|
|
ReprLeaf::from_singleton_buffer( SingletonBuffer::new('5') )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let digit_char_buf = rt_digit
|
|
|
|
|
.descend(Context::parse(&ctx, "Char")).unwrap()
|
|
|
|
|
.singleton_buffer::<char>();
|
|
|
|
|
let digit_char_view = rt_digit
|
|
|
|
|
.descend(Context::parse(&ctx, "Char")).unwrap()
|
|
|
|
|
.view_char();
|
|
|
|
|
|
|
|
|
|
// before setting up the morphism, char-view remains as initialized
|
|
|
|
|
assert_eq!( digit_char_buf.get(), '5' );
|
|
|
|
|
assert_eq!( digit_char_view.get_view().unwrap().get(), '5' );
|
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
// now we attach the char-repr to the u64-repr
|
2024-03-15 18:54:25 +01:00
|
|
|
|
ctx.read().unwrap().morphisms.apply_morphism(
|
|
|
|
|
rt_digit.clone(),
|
2024-05-25 19:21:16 +02:00
|
|
|
|
&Context::parse(&ctx, "<Digit 16>~ℤ_2^64~machine.UInt64"),
|
2024-03-15 18:54:25 +01:00
|
|
|
|
&Context::parse(&ctx, "<Digit 16>~Char")
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
// char buffer and view should now follow the u64-buffer
|
2024-03-15 18:54:25 +01:00
|
|
|
|
assert_eq!( digit_char_view.get_view().unwrap().get(), '8' );
|
|
|
|
|
assert_eq!( digit_char_buf.get(), '8' );
|
|
|
|
|
|
2024-05-25 19:21:16 +02:00
|
|
|
|
// now u64-buffer changes, and char-buffer should change accordingly
|
|
|
|
|
digit_u64_buffer.set(3);
|
|
|
|
|
assert_eq!( digit_u64_buffer.get(), 3 );
|
2024-03-15 18:54:25 +01:00
|
|
|
|
|
|
|
|
|
// char buffer should follow
|
|
|
|
|
digit_char_view.0.update();
|
|
|
|
|
assert_eq!( digit_char_buf.get(), '3' );
|
|
|
|
|
assert_eq!( digit_char_view.get_view().unwrap().get(), '3' );
|
|
|
|
|
}
|
|
|
|
|
|