Files @ 233468b578e8
Branch filter:

Location: CSY/reowolf/src/runtime2/tests/basics.rs

233468b578e8 3.8 KiB application/rls-services+xml Show Annotation Show as Raw Download as Raw
MH
WIP on handling messages intended for future sync block of failing component

use super::*;

#[test]
fn test_doing_nothing() {
    // If this thing does not get into an infinite loop, (hence: the runtime
    // exits), then the test works
    const CODE: &'static str ="
    primitive silent_willy(u32 loops) {
        u32 index = 0;
        while (index < loops) {
            sync { index += 1; }
        }
    }
    ";

    let thing = TestTimer::new("doing_nothing");
    run_test_in_runtime(CODE, |api| {
        api.create_connector("", "silent_willy", ValueGroup::new_stack(vec![
            Value::UInt32(NUM_LOOPS),
        ])).expect("create component");
    });
}

#[test]
fn test_local_sync_failure() {
    // If the component exits cleanly, then the runtime exits cleanly, and the
    // test will finish
    const CODE: &'static str = "
    primitive immediate_failure() {
        u32[] only_allows_index_0 = { 1 };
        while (true) sync { // note the infinite loop
            auto value = only_allows_index_0[1];
        }
    }
    ";

    let thing = TestTimer::new("immediate_local_failure");
    run_test_in_runtime(CODE, |api| {
        api.create_connector("", "immediate_failure", ValueGroup::new_stack(Vec::new()))
            .expect("create component");
    })
}



#[test]
fn test_single_put_and_get() {
    const CODE: &'static str = "
    primitive putter(out<bool> sender, u32 loops) {
        u32 index = 0;
        while (index < loops) {
            sync {
                put(sender, true);
            }
            index += 1;
        }
    }

    primitive getter(in<bool> receiver, u32 loops) {
        u32 index = 0;
        while (index < loops) {
            sync {
                auto result = get(receiver);
                assert(result);
            }
            index += 1;
        }
    }
    ";

    let thing = TestTimer::new("single_put_and_get");
    run_test_in_runtime(CODE, |api| {
        let channel = api.create_channel().unwrap();

        api.create_connector("", "putter", ValueGroup::new_stack(vec![
            Value::Output(PortId(Id{ connector_id: 0, u32_suffix: channel.putter_id.index })),
            Value::UInt32(NUM_LOOPS)
        ])).expect("create putter");

        api.create_connector("", "getter", ValueGroup::new_stack(vec![
            Value::Input(PortId(Id{ connector_id: 0, u32_suffix: channel.getter_id.index })),
            Value::UInt32(NUM_LOOPS)
        ])).expect("create getter");
    });
}

#[test]
fn test_multi_put_and_get() {
    const CODE: &'static str = "
    primitive putter_static(out<u8> vals, u32 num_loops) {
        u32 index = 0;
        while (index < num_loops) {
            sync {
                put(vals, 0b00000001);
                put(vals, 0b00000100);
                put(vals, 0b00010000);
                put(vals, 0b01000000);
            }
            index += 1;
        }
    }

    primitive getter_dynamic(in<u8> vals, u32 num_loops) {
        u32 loop_index = 0;
        while (loop_index < num_loops) {
            sync {
                u32 recv_index = 0;
                u8 expected = 1;
                while (recv_index < 4) {
                    auto gotten = get(vals);
                    assert(gotten == expected);
                    expected <<= 2;
                    recv_index += 1;
                }
            }
            loop_index += 1;
        }
    }
    ";

    let thing = TestTimer::new("multi_put_and_get");
    run_test_in_runtime(CODE, |api| {
        let channel = api.create_channel().unwrap();
        api.create_connector("", "putter_static", ValueGroup::new_stack(vec![
            Value::Output(PortId::new(channel.putter_id.index)),
            Value::UInt32(NUM_LOOPS),
        ])).unwrap();
        api.create_connector("", "getter_dynamic", ValueGroup::new_stack(vec![
            Value::Input(PortId::new(channel.getter_id.index)),
            Value::UInt32(NUM_LOOPS),
        ])).unwrap();
    })
}