arcweight 0.3.0

A high-performance, modular library for weighted finite state transducers with comprehensive examples and benchmarks
Documentation
//! Tests for FST construction macros

#[macro_use]
extern crate arcweight;

use arcweight::prelude::*;

#[test]
fn test_fst_macro_basic() {
    let fst = fst!(
        start: 0,
        finals: [1],
        arcs: [
            (0, 1, 1, 1, 1.0),
        ]
    );

    assert_eq!(fst.num_states(), 2);
    assert_eq!(fst.start(), Some(0));
    assert!(fst.is_final(1));
    assert_eq!(fst.num_arcs(0), 1);
}

#[test]
fn test_fst_macro_multiple_arcs() {
    let fst = fst!(
        start: 0,
        finals: [2],
        arcs: [
            (0, 1, 1, 1, 1.0),
            (1, 2, 2, 2, 2.0),
        ]
    );

    assert_eq!(fst.num_states(), 3);
    assert_eq!(fst.num_arcs(0), 1);
    assert_eq!(fst.num_arcs(1), 1);
}

#[test]
fn test_fst_macro_multiple_finals() {
    let fst = fst!(
        start: 0,
        finals: [1, 2],
        arcs: [
            (0, 1, 1, 1, 1.0),
            (0, 2, 2, 2, 2.0),
        ]
    );

    assert!(fst.is_final(1));
    assert!(fst.is_final(2));
}

#[test]
fn test_fst_macro_complex() {
    let fst = fst!(
        start: 0,
        finals: [3, 4],
        arcs: [
            (0, 1, 1, 10, 1.0),
            (1, 2, 2, 20, 2.0),
            (2, 3, 3, 30, 3.0),
            (0, 4, 4, 40, 4.0),
        ]
    );

    assert_eq!(fst.num_states(), 5);
    assert_eq!(fst.num_arcs(0), 2);
    assert_eq!(fst.num_arcs(1), 1);
    assert_eq!(fst.num_arcs(2), 1);
}

#[test]
fn test_symt_macro_basic() {
    let symbols = symt!["hello", "world", "test"];

    assert_eq!(symbols.find_id("hello"), Some(1));
    assert_eq!(symbols.find_id("world"), Some(2));
    assert_eq!(symbols.find_id("test"), Some(3));
    assert_eq!(symbols.find(1), Some("hello"));
}

#[test]
fn test_symt_macro_empty() {
    let symbols = symt![];
    assert_eq!(symbols.len(), 1); // Epsilon symbol
}

#[test]
fn test_symt_macro_single() {
    let symbols = symt!["single"];
    assert_eq!(symbols.find_id("single"), Some(1));
}

#[test]
fn test_symt_macro_many() {
    let symbols = symt!["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
    assert_eq!(symbols.len(), 11); // 10 + epsilon
    assert_eq!(symbols.find_id("j"), Some(10));
}

#[test]
fn test_fst_path_macro() {
    let path = fst_path!(
        states: [0, 1, 2],
        arcs: [
            (1, 10, 1.0),
            (2, 20, 0.5),
        ],
        weight: 1.5
    );

    assert_eq!(path.states, vec![0, 1, 2]);
    assert_eq!(path.arcs.len(), 2);
    assert_eq!(path.input_labels(), vec![1, 2]);
    assert_eq!(path.output_labels(), vec![10, 20]);
}

#[test]
fn test_fst_path_macro_single_arc() {
    let path = fst_path!(
        states: [0, 1],
        arcs: [
            (1, 1, 1.0),
        ],
        weight: 1.0
    );

    assert_eq!(path.states.len(), 2);
    assert_eq!(path.arcs.len(), 1);
}

#[test]
fn test_fst_path_macro_no_arcs() {
    let path = fst_path!(
        states: [0],
        arcs: [],
        weight: 0.0
    );

    assert_eq!(path.states, vec![0]);
    assert_eq!(path.arcs.len(), 0);
}

#[test]
fn test_fst_macro_with_epsilon() {
    let fst = fst!(
        start: 0,
        finals: [2],
        arcs: [
            (0, 1, 0, 0, 1.0), // epsilon
            (1, 2, 1, 1, 1.0),
        ]
    );

    assert_eq!(fst.num_states(), 3);
    assert_eq!(fst.num_arcs(0), 1);
}

#[test]
fn test_fst_macro_self_loop() {
    let fst = fst!(
        start: 0,
        finals: [0],
        arcs: [
            (0, 0, 1, 1, 1.0), // self loop
        ]
    );

    assert_eq!(fst.num_states(), 1);
    assert_eq!(fst.num_arcs(0), 1);
    assert!(fst.is_final(0));
}

#[test]
fn test_fst_macro_high_state_ids() {
    let fst = fst!(
        start: 10,
        finals: [20],
        arcs: [
            (10, 15, 1, 1, 1.0),
            (15, 20, 2, 2, 2.0),
        ]
    );

    assert_eq!(fst.num_states(), 21); // 0-20
    assert_eq!(fst.start(), Some(10));
    assert!(fst.is_final(20));
}

#[test]
fn test_fst_macro_different_weights() {
    let fst = fst!(
        start: 0,
        finals: [2],
        arcs: [
            (0, 1, 1, 1, 0.5),
            (1, 2, 2, 2, 1.5),
        ]
    );

    // Verify arcs have correct weights
    let mut arc_iter = fst.arcs(0);
    let arc = arc_iter.next().unwrap();
    assert_eq!(arc.weight, TropicalWeight::new(0.5));
}

#[test]
fn test_symt_macro_unicode() {
    let symbols = symt!["hello", "世界", "test"];
    assert_eq!(symbols.find_id("世界"), Some(2));
    assert_eq!(symbols.find(2), Some("世界"));
}

#[test]
fn test_fst_macro_large() {
    // Test macro with multiple arcs
    let fst = fst!(
        start: 0,
        finals: [5],
        arcs: [
            (0, 1, 0, 0, 1.0),
            (1, 2, 1, 1, 1.0),
            (2, 3, 2, 2, 1.0),
            (3, 4, 3, 3, 1.0),
            (4, 5, 4, 4, 1.0),
        ]
    );

    assert_eq!(fst.num_states(), 6);
}