logic-circus 0.3.0

Open source logic circuit simualtor written in Rust.
Documentation
use std::collections::HashMap;

use crate::{Component, Gate, GateLike};

use super::*;

macro_rules! table {
    ($($($key:expr),* $(,)? => $($value:expr),* $(,)? );* $(;)?) => {{
        let mut map = HashMap::new();
        $(map.insert(vec![$($key),*], vec![$($value),*]);)*
        map
    }};
}

fn check(mut component: Component, truth_table: HashMap<Vec<Bit>, Vec<Bit>>) {
    for (input, output) in truth_table {
        // SAFETY: I wrote this so it must be safe
        assert_eq!(unsafe { component.compute(input) }, output);
        component.reset(false);
    }
}

fn component(gate: Gate) -> Component {
    Component::single_gate(gate, 2)
}

#[test]
fn nand() {
    check(
        component(Gate::nand()),
        table! {
                false, false => true;
                false, true => true;
                true, false => true;
                true, true => false;
        },
    )
}

#[test]
fn dup() {
    check(
        Component::single_gate(Gate::dup(3), 3),
        table! {
            false => false, false, false;
            true => true, true, true;
        },
    )
}

#[test]
fn not() {
    check(
        component(Gate::not()),
        table! {
            true => false;
            false => true;
        },
    )
}

#[test]
fn and() {
    check(
        component(Gate::and()),
        table! {
            false, false => false;
            false, true => false;
            true, false => false;
            true, true => true;
        },
    )
}

#[test]
fn or() {
    check(
        component(Gate::or()),
        table! {
            false, false => false;
            false, true => true;
            true, false => true;
            true, true => true;
        },
    )
}

#[test]
fn nor() {
    check(
        component(Gate::nor()),
        table! {
            false, false => true;
            false, true => false;
            true, false => false;
            true, true => false;
        },
    )
}

#[test]
fn xor() {
    check(
        component(Gate::xor()),
        table! {
            false, false => false;
            false, true => true;
            true, false => true;
            true, true => false;
        },
    )
}