avmnif-rs 0.4.1

Safe NIF toolkit for AtomVM written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Test helper functions and utilities
//! 
//! This module provides common utilities and helper functions that make
//! writing tests easier and more consistent across the codebase.
//!
//! # Design Philosophy
//!
//! All helpers are generic and work with any AtomTableOps implementation.
//! No global state, no hardcoded dependencies - pure dependency injection.

use alloc::vec;
use alloc::vec::Vec;
use alloc::format;
use alloc::string::ToString;

use crate::atom::AtomTableOps;
use crate::term::TermValue;

// ── Generic Atom Creation Helpers ──────────────────────────────────────────

/// Create an atom using any atom table
pub fn atom<T: AtomTableOps>(name: &str, table: &T) -> TermValue {
    TermValue::atom(name, table)
}

/// Create multiple atoms at once
pub fn atoms<T: AtomTableOps>(names: &[&str], table: &T) -> Vec<TermValue> {
    names.iter()
        .map(|name| TermValue::atom(name, table))
        .collect()
}

// ── Generic Term Creation Helpers ──────────────────────────────────────────

/// Create a list of integers for testing
pub fn int_list(values: &[i32]) -> TermValue {
    let elements: Vec<TermValue> = values.iter()
        .map(|&v| TermValue::int(v))
        .collect();
    TermValue::list(elements)
}

/// Create a tuple of integers for testing
pub fn int_tuple(values: &[i32]) -> TermValue {
    let elements: Vec<TermValue> = values.iter()
        .map(|&v| TermValue::int(v))
        .collect();
    TermValue::tuple(elements)
}

/// Create a map with atom keys and mixed values
pub fn atom_map<T: AtomTableOps>(
    pairs: &[(&str, TermValue)], 
    table: &T
) -> TermValue {
    let map_pairs: Vec<(TermValue, TermValue)> = pairs.iter()
        .map(|(key_name, value)| (TermValue::atom(key_name, table), value.clone()))
        .collect();
    TermValue::map(map_pairs)
}

/// Create test data for complex nested structures
pub fn create_complex_test_data<T: AtomTableOps>(table: &T) -> TermValue {
    TermValue::map(vec![
        (
            TermValue::atom("user", table),
            TermValue::map(vec![
                (TermValue::atom("name", table), TermValue::atom("alice", table)),
                (TermValue::atom("age", table), TermValue::int(30)),
                (TermValue::atom("active", table), TermValue::atom("true", table)),
                (
                    TermValue::atom("permissions", table),
                    TermValue::list(vec![
                        TermValue::atom("read", table),
                        TermValue::atom("write", table),
                        TermValue::atom("admin", table),
                    ])
                ),
            ])
        ),
        (
            TermValue::atom("session", table),
            TermValue::tuple(vec![
                TermValue::atom("session_id", table),
                TermValue::int(12345),
                TermValue::atom("authenticated", table),
            ])
        ),
        (
            TermValue::atom("metadata", table),
            TermValue::map(vec![
                (TermValue::atom("version", table), TermValue::int(1)),
                (TermValue::atom("created_at", table), TermValue::int(1640995200)), // Unix timestamp
                (
                    TermValue::atom("tags", table),
                    TermValue::list(vec![
                        TermValue::atom("production", table),
                        TermValue::atom("verified", table),
                    ])
                ),
            ])
        ),
    ])
}

// ── Generic Assertion Helpers ──────────────────────────────────────────────

/// Assert that two TermValues are equal with detailed error messages
/// 
/// Provides better debugging information than standard assert_eq!
/// when comparing complex TermValue structures.
pub fn assert_term_eq(left: &TermValue, right: &TermValue) {
    if left != right {
        panic!(
            "TermValue assertion failed:\nLeft:  {:?}\nRight: {:?}",
            left, right
        );
    }
}

/// Assert that a TermValue is an atom with the given name
pub fn assert_atom_str<T: AtomTableOps>(
    term: &TermValue, 
    expected: &str, 
    table: &T
) {
    match term {
        TermValue::Atom(idx) => {
            if !table.atom_equals_str(*idx, expected) {
                let actual = term.as_atom_str(table)
                    .unwrap_or_else(|| format!("unknown({})", idx.0));
                panic!(
                    "Atom assertion failed: expected '{}', got '{}'",
                    expected, actual
                );
            }
        }
        _ => panic!(
            "Expected atom '{}', got non-atom term: {:?}",
            expected, term
        ),
    }
}

/// Assert that a TermValue is an integer with the given value
pub fn assert_int(term: &TermValue, expected: i32) {
    match term {
        TermValue::SmallInt(actual) => {
            if *actual != expected {
                panic!(
                    "Integer assertion failed: expected {}, got {}",
                    expected, actual
                );
            }
        }
        _ => panic!(
            "Expected integer {}, got non-integer term: {:?}",
            expected, term
        ),
    }
}

/// Assert that a TermValue is a list with the given length
pub fn assert_list_length(term: &TermValue, expected_length: usize) {
    let actual_length = term.list_length();
    if actual_length != expected_length {
        panic!(
            "List length assertion failed: expected {}, got {}. Term: {:?}",
            expected_length, actual_length, term
        );
    }
}

/// Assert that a TermValue is a tuple with the given arity
pub fn assert_tuple_arity(term: &TermValue, expected_arity: usize) {
    let actual_arity = term.tuple_arity();
    if actual_arity != expected_arity {
        panic!(
            "Tuple arity assertion failed: expected {}, got {}. Term: {:?}",
            expected_arity, actual_arity, term
        );
    }
}

/// Assert that a map contains a key
pub fn assert_map_has_key<T: AtomTableOps>(
    map: &TermValue,
    key_name: &str,
    table: &T
) {
    let key = TermValue::atom(key_name, table);
    if map.map_get(&key).is_none() {
        panic!(
            "Map assertion failed: expected key '{}' to exist in map: {:?}",
            key_name, map
        );
    }
}

/// Assert that a map has a specific key-value pair
pub fn assert_map_contains<T: AtomTableOps>(
    map: &TermValue,
    key_name: &str,
    expected_value: &TermValue,
    table: &T
) {
    let key = TermValue::atom(key_name, table);
    match map.map_get(&key) {
        Some(actual_value) => {
            if actual_value != expected_value {
                panic!(
                    "Map value assertion failed for key '{}': expected {:?}, got {:?}",
                    key_name, expected_value, actual_value
                );
            }
        }
        None => panic!(
            "Map key assertion failed: key '{}' not found in map: {:?}",
            key_name, map
        ),
    }
}

// ── Generic Testing Utilities ──────────────────────────────────────────────

/// Test that a function correctly handles all common atom types
pub fn test_with_common_atoms<T: AtomTableOps, F>(
    table: &T,
    mut test_fn: F
) 
where 
    F: FnMut(&str, TermValue),
{
    let common_atoms = [
        "ok", "error", "true", "false", "undefined", "badarg", "nil",
        "atom", "binary", "boolean", "float", "function", "integer",
        "list", "map", "pid", "port", "reference", "tuple"
    ];
    
    for &atom_name in &common_atoms {
        let atom_term = TermValue::atom(atom_name, table);
        test_fn(atom_name, atom_term);
    }
}

/// Benchmark helper - measure time for an operation
/// 
/// Note: This is a no-op in no_std environments. 
/// Returns the result and 0 for elapsed time.
pub fn time_operation<F, R>(operation: F) -> (R, u128)
where
    F: FnOnce() -> R,
{
    let result = operation();
    // In no_std, we can't measure time, so return 0
    (result, 0)
}

/// Create a test user fixture
pub fn create_user_fixture<T: AtomTableOps>(
    name: &str, 
    id: i32, 
    role: &str,
    table: &T
) -> TermValue {
    TermValue::map(vec![
        (TermValue::atom("name", table), TermValue::atom(name, table)),
        (TermValue::atom("id", table), TermValue::int(id)),
        (TermValue::atom("role", table), TermValue::atom(role, table)),
        (TermValue::atom("active", table), TermValue::atom("true", table)),
    ])
}

/// Create a test config fixture
pub fn create_config_fixture<T: AtomTableOps>(table: &T) -> TermValue {
    TermValue::map(vec![
        (TermValue::atom("host", table), TermValue::atom("localhost", table)),
        (TermValue::atom("port", table), TermValue::int(8080)),
        (TermValue::atom("ssl", table), TermValue::atom("false", table)),
        (
            TermValue::atom("database", table),
            TermValue::map(vec![
                (TermValue::atom("host", table), TermValue::atom("db.example.com", table)),
                (TermValue::atom("port", table), TermValue::int(5432)),
                (TermValue::atom("name", table), TermValue::atom("myapp", table)),
            ])
        ),
    ])
}

/// Create test statistics fixture
pub fn create_stats_fixture<T: AtomTableOps>(table: &T) -> TermValue {
    TermValue::map(vec![
        (TermValue::atom("requests_total", table), TermValue::int(1000)),
        (TermValue::atom("errors_total", table), TermValue::int(5)),
        (TermValue::atom("uptime_seconds", table), TermValue::int(86400)),
        (TermValue::atom("memory_mb", table), TermValue::int(512)),
    ])
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::mocks::MockAtomTable;

    #[test]
    fn test_generic_atom_creation() {
        let table = MockAtomTable::new();
        
        let hello_atom = atom("hello", &table);
        let world_atom = atom("world", &table);
        
        assert_atom_str(&hello_atom, "hello", &table);
        assert_atom_str(&world_atom, "world", &table);
    }

    #[test]
    fn test_multiple_atoms() {
        let table = MockAtomTable::new();
        
        let atom_terms = atoms(&["red", "green", "blue"], &table);
        assert_eq!(atom_terms.len(), 3);
        
        assert_atom_str(&atom_terms[0], "red", &table);
        assert_atom_str(&atom_terms[1], "green", &table);
        assert_atom_str(&atom_terms[2], "blue", &table);
    }

    #[test]
    fn test_helper_assertions() {
        let table = MockAtomTable::new();
        
        let int_term = TermValue::int(42);
        let atom_term = atom("test", &table);
        let list_term = int_list(&[1, 2, 3]);
        let tuple_term = int_tuple(&[10, 20]);
        
        assert_int(&int_term, 42);
        assert_atom_str(&atom_term, "test", &table);
        assert_list_length(&list_term, 3);
        assert_tuple_arity(&tuple_term, 2);
    }

    #[test]
    fn test_complex_test_data() {
        let table = MockAtomTable::new();
        let data = create_complex_test_data(&table);
        
        // Should be a map with user, session, and metadata
        let user_key = TermValue::atom("user", &table);
        let session_key = TermValue::atom("session", &table);
        let metadata_key = TermValue::atom("metadata", &table);
        
        let user = data.map_get(&user_key).unwrap();
        let session = data.map_get(&session_key).unwrap();
        let metadata = data.map_get(&metadata_key).unwrap();
        
        // Verify structure
        let name_key = TermValue::atom("name", &table);
        let version_key = TermValue::atom("version", &table);
        assert!(user.map_get(&name_key).is_some());
        assert_tuple_arity(session, 3);
        assert!(metadata.map_get(&version_key).is_some());
    }

    #[test]
    fn test_time_operation() {
        let (result, _time) = time_operation(|| {
            // In no_std, we can't actually sleep or measure time
            42
        });
        
        assert_eq!(result, 42);
        // Don't assert on time in no_std environment
    }

    #[test] 
    fn test_common_atoms() {
        let table = MockAtomTable::new();
        let mut count = 0;
        
        test_with_common_atoms(&table, |name, term| {
            assert_atom_str(&term, name, &table);
            count += 1;
        });
        
        assert!(count > 10); // Should test many atoms
    }

    #[test]
    fn test_map_assertions() {
        let table = MockAtomTable::new();
        
        let test_map = TermValue::map(vec![
            (TermValue::atom("name", &table), TermValue::atom("alice", &table)),
            (TermValue::atom("age", &table), TermValue::int(30)),
        ]);
        
        // Test map has key
        assert_map_has_key(&test_map, "name", &table);
        assert_map_has_key(&test_map, "age", &table);
        
        // Test map contains specific values
        assert_map_contains(&test_map, "name", &TermValue::atom("alice", &table), &table);
        assert_map_contains(&test_map, "age", &TermValue::int(30), &table);
    }

    #[test]
    fn test_fixture_creation() {
        let table = MockAtomTable::new();
        
        // Test user fixture
        let user = create_user_fixture("bob", 123, "admin", &table);
        assert_map_has_key(&user, "name", &table);
        assert_map_has_key(&user, "id", &table);
        assert_map_has_key(&user, "role", &table);
        
        // Test config fixture
        let config = create_config_fixture(&table);
        assert_map_has_key(&config, "host", &table);
        assert_map_has_key(&config, "port", &table);
        assert_map_has_key(&config, "database", &table);
        
        // Test stats fixture
        let stats = create_stats_fixture(&table);
        assert_map_has_key(&stats, "requests_total", &table);
        assert_map_contains(&stats, "requests_total", &TermValue::int(1000), &table);
    }
}