1
use nomiscript::{
2
    Closure, Fraction, Number, Pair, Symbol, SymbolKind, SymbolTable, Value, format_value,
3
    list_to_vec, vec_to_list,
4
};
5

            
6
211159
fn num(n: i64) -> Value {
7
211159
    Value::Number(Number::Index(n))
8
211159
}
9

            
10
7
fn frac(n: i64, d: i64) -> Value {
11
7
    Value::Number(Number::Scalar(Fraction::new(n, d)))
12
7
}
13

            
14
mod value_types {
15
    use super::*;
16

            
17
    #[test]
18
1
    fn test_type_names() {
19
1
        let cases = [
20
1
            (Value::Nil, "nil"),
21
1
            (Value::T, "boolean"),
22
1
            (num(42), "number"),
23
1
            (Value::String("hello".into()), "string"),
24
1
            (Value::Symbol("x".into()), "symbol"),
25
1
            (
26
1
                Value::Pair(Box::new(Pair::new(Value::Nil, Value::Nil))),
27
1
                "pair",
28
1
            ),
29
1
            (Value::Vector(vec![]), "vector"),
30
1
            (Value::Closure(Closure::new(0, vec![])), "closure"),
31
1
        ];
32

            
33
1
        for (value, expected_type) in cases {
34
8
            assert_eq!(value.type_name(), expected_type);
35
        }
36
1
    }
37

            
38
    #[test]
39
1
    fn test_truthiness() {
40
1
        assert!(Value::Nil.is_nil());
41

            
42
1
        let truthy = [
43
1
            Value::T,
44
1
            num(0),
45
1
            num(1),
46
1
            Value::String(String::new()),
47
1
            Value::String("hello".into()),
48
1
            Value::Symbol("x".into()),
49
1
            Value::Pair(Box::new(Pair::new(Value::Nil, Value::Nil))),
50
1
            Value::Vector(vec![]),
51
1
            Value::Closure(Closure::new(0, vec![])),
52
1
        ];
53

            
54
1
        for val in truthy {
55
9
            assert!(!val.is_nil(), "{val:?} should be truthy");
56
        }
57
1
    }
58
}
59

            
60
mod fractions {
61
    use super::*;
62

            
63
    #[test]
64
1
    fn test_fraction_equality() {
65
1
        assert_eq!(frac(1, 2), frac(2, 4));
66
1
        assert_eq!(frac(3, 6), frac(1, 2));
67
1
        assert_eq!(frac(100, 100), frac(1, 1));
68
1
    }
69

            
70
    #[test]
71
1
    fn test_fraction_normalization() {
72
1
        let half = Fraction::new(1, 2);
73
1
        let also_half = Fraction::new(50, 100);
74
1
        assert_eq!(half, also_half);
75

            
76
1
        let negative = Fraction::new(-1, 2);
77
1
        let also_negative = Fraction::new(1, -2);
78
1
        assert_eq!(negative, also_negative);
79
1
    }
80
}
81

            
82
mod pairs {
83
    use super::*;
84

            
85
    #[test]
86
1
    fn test_cons() {
87
1
        let pair = Pair::cons(num(1), num(2));
88
1
        match pair {
89
            Value::Pair(p) => {
90
1
                assert_eq!(p.car, num(1));
91
1
                assert_eq!(p.cdr, num(2));
92
            }
93
            _ => panic!("expected pair"),
94
        }
95
1
    }
96

            
97
    #[test]
98
1
    fn test_nested_pairs() {
99
1
        let inner = Pair::cons(num(2), num(3));
100
1
        let outer = Pair::cons(num(1), inner);
101

            
102
1
        match outer {
103
            Value::Pair(p) => {
104
1
                assert_eq!(p.car, num(1));
105
1
                match &p.cdr {
106
                    Value::Pair(inner_p) => {
107
1
                        assert_eq!(inner_p.car, num(2));
108
1
                        assert_eq!(inner_p.cdr, num(3));
109
                    }
110
                    _ => panic!("expected inner pair"),
111
                }
112
            }
113
            _ => panic!("expected outer pair"),
114
        }
115
1
    }
116
}
117

            
118
mod lists {
119
    use super::*;
120

            
121
    #[test]
122
1
    fn test_empty_list() {
123
1
        let list = Value::Nil;
124
1
        let vec = list_to_vec(&list).unwrap();
125
1
        assert!(vec.is_empty());
126

            
127
1
        let back = vec_to_list(vec);
128
1
        assert_eq!(back, Value::Nil);
129
1
    }
130

            
131
    #[test]
132
1
    fn test_single_element_list() {
133
1
        let list = Pair::cons(num(42), Value::Nil);
134
1
        let vec = list_to_vec(&list).unwrap();
135
1
        assert_eq!(vec, vec![num(42)]);
136

            
137
1
        let back = vec_to_list(vec);
138
1
        assert_eq!(back, list);
139
1
    }
140

            
141
    #[test]
142
1
    fn test_multi_element_list() {
143
1
        let list = Pair::cons(num(1), Pair::cons(num(2), Pair::cons(num(3), Value::Nil)));
144

            
145
1
        let vec = list_to_vec(&list).unwrap();
146
1
        assert_eq!(vec, vec![num(1), num(2), num(3)]);
147

            
148
1
        let back = vec_to_list(vec);
149
1
        assert_eq!(back, list);
150
1
    }
151

            
152
    #[test]
153
1
    fn test_improper_list() {
154
1
        let improper = Pair::cons(num(1), num(2));
155
1
        assert!(list_to_vec(&improper).is_none());
156

            
157
1
        let improper = Pair::cons(num(1), Pair::cons(num(2), num(3)));
158
1
        assert!(list_to_vec(&improper).is_none());
159
1
    }
160

            
161
    #[test]
162
1
    fn test_heterogeneous_list() {
163
1
        let list = Pair::cons(
164
1
            num(1),
165
1
            Pair::cons(
166
1
                Value::String("hello".into()),
167
1
                Pair::cons(Value::T, Value::Nil),
168
1
            ),
169
1
        );
170

            
171
1
        let vec = list_to_vec(&list).unwrap();
172
1
        assert_eq!(vec.len(), 3);
173
1
        assert_eq!(vec[0], num(1));
174
1
        assert_eq!(vec[1], Value::String("hello".into()));
175
1
        assert_eq!(vec[2], Value::T);
176
1
    }
177

            
178
    #[test]
179
1
    fn test_nested_list() {
180
1
        let inner = Pair::cons(num(2), Pair::cons(num(3), Value::Nil));
181
1
        let outer = Pair::cons(num(1), Pair::cons(inner.clone(), Value::Nil));
182

            
183
1
        let vec = list_to_vec(&outer).unwrap();
184
1
        assert_eq!(vec.len(), 2);
185
1
        assert_eq!(vec[0], num(1));
186
1
        assert_eq!(vec[1], inner);
187
1
    }
188

            
189
    #[test]
190
1
    fn test_roundtrip_preserves_structure() {
191
1
        let original = vec![num(1), Value::String("test".into()), Value::Nil, frac(1, 3)];
192

            
193
1
        let as_list = vec_to_list(original.clone());
194
1
        let back = list_to_vec(&as_list).unwrap();
195

            
196
1
        assert_eq!(original, back);
197
1
    }
198
}
199

            
200
mod symbols {
201
    use super::*;
202

            
203
    #[test]
204
1
    fn test_symbol_creation() {
205
1
        let sym = Symbol::new("foo", SymbolKind::Variable);
206
1
        assert_eq!(sym.name(), "foo");
207
1
        assert_eq!(sym.kind(), SymbolKind::Variable);
208
1
    }
209

            
210
    #[test]
211
1
    fn test_symbol_table() {
212
1
        let mut table = SymbolTable::new();
213
1
        table.define(Symbol::new("+", SymbolKind::Operator));
214
1
        table.define(Symbol::new("car", SymbolKind::Native));
215

            
216
1
        assert!(table.contains("+"));
217
1
        assert!(table.contains("car"));
218
1
        assert!(!table.contains("undefined"));
219

            
220
1
        assert_eq!(table.lookup("+").unwrap().kind(), SymbolKind::Operator);
221
1
        assert_eq!(table.lookup("car").unwrap().kind(), SymbolKind::Native);
222
1
    }
223

            
224
    #[test]
225
1
    fn test_symbol_table_builtins() {
226
1
        let table = SymbolTable::with_builtins();
227

            
228
1
        assert!(table.contains("+"));
229
1
        assert!(table.contains("IF"));
230
1
        assert!(table.contains("CAR"));
231

            
232
1
        assert_eq!(table.lookup("+").unwrap().kind(), SymbolKind::Operator);
233
1
        assert_eq!(table.lookup("IF").unwrap().kind(), SymbolKind::SpecialForm);
234
1
        assert_eq!(table.lookup("CAR").unwrap().kind(), SymbolKind::Native);
235
1
    }
236
}
237

            
238
mod closures {
239
    use super::*;
240

            
241
    #[test]
242
1
    fn test_closure_creation() {
243
1
        let closure = Closure::new(42, vec![num(1), num(2)]);
244
1
        assert_eq!(closure.code_id, Some(42));
245
1
        assert_eq!(closure.env, vec![num(1), num(2)]);
246
1
    }
247

            
248
    #[test]
249
1
    fn test_empty_closure() {
250
1
        let closure = Closure::new(0, vec![]);
251
1
        assert_eq!(closure.code_id, Some(0));
252
1
        assert!(closure.env.is_empty());
253
1
    }
254
}
255

            
256
mod large_structures {
257
    use super::*;
258

            
259
    #[test]
260
1
    fn test_long_list() {
261
1
        let values: Vec<Value> = (0..1000).map(num).collect();
262
1
        let list = vec_to_list(values.clone());
263
1
        let back = list_to_vec(&list).unwrap();
264
1
        assert_eq!(values, back);
265
1
    }
266

            
267
    #[test]
268
1
    fn test_deeply_nested_pairs() {
269
1
        let mut val = num(0);
270
1
        for i in 1..100 {
271
99
            val = Pair::cons(num(i), val);
272
        }
273

            
274
1
        match &val {
275
1
            Value::Pair(p) => assert_eq!(p.car, num(99)),
276
            _ => panic!("expected pair"),
277
        }
278
1
    }
279

            
280
    #[test]
281
1
    fn test_large_vector() {
282
1
        let vec: Vec<Value> = (0..10000).map(num).collect();
283
1
        let val = Value::Vector(vec.clone());
284

            
285
1
        match val {
286
1
            Value::Vector(v) => assert_eq!(v.len(), 10000),
287
            _ => panic!("expected vector"),
288
        }
289
1
    }
290

            
291
    #[test]
292
1
    fn long_list_survives_a_small_stack() {
293
1
        let handle = std::thread::Builder::new()
294
1
            .stack_size(256 * 1024)
295
1
            .spawn(|| {
296
1
                let list = vec_to_list((0..100_000).map(num).collect());
297
1
                let copy = list.clone();
298
1
                let other = vec_to_list((0..99_999).chain([-1]).map(num).collect());
299
1
                assert!(copy == list, "clone differs");
300
1
                assert!(copy != other, "different lists compare equal");
301
1
                assert_eq!(list_to_vec(&copy).map(|v| v.len()), Some(100_000));
302
1
                assert!(format_value(&copy).ends_with("99999)"));
303
1
            })
304
1
            .expect("spawn");
305
1
        handle.join().expect("no stack overflow");
306
1
    }
307

            
308
    #[test]
309
1
    fn improper_tail_is_cloned_and_compared() {
310
2
        let improper = |tail| Pair::cons(num(1), Pair::cons(num(2), tail));
311
1
        let list = improper(num(3));
312
1
        assert!(list.clone() == list);
313
1
        assert!(list != improper(num(4)));
314
1
        assert!(list != vec_to_list(vec![num(1), num(2), num(3)]));
315
1
    }
316

            
317
    #[test]
318
1
    fn nested_car_is_cloned_and_compared() {
319
4
        let nested = |second| vec_to_list(vec![vec_to_list(vec![num(1), num(second)]), num(3)]);
320
1
        assert!(nested(2).clone() == nested(2));
321
1
        assert!(nested(2) != nested(9));
322
1
    }
323
}