pushr 0.4.1

Pushr is a Rust based interpreter for Push programs.
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use crate::push::instructions::{Instruction, InstructionCache};
use crate::push::item::Item;
use crate::push::item::PushType;
use crate::push::state::*;
use crate::push::topology::Topology;
use crate::push::vector::{BoolVector, FloatVector, IntVector};
use std::collections::HashMap;

/// Integer numbers (that is, numbers without decimal points).
pub fn load_list_instructions(map: &mut HashMap<String, Instruction>) {
    map.insert(String::from("LIST.ADD"), Instruction::new(list_add));
    map.insert(String::from("LIST.REMOVE"), Instruction::new(list_remove));
    map.insert(String::from("LIST.GET"), Instruction::new(list_get));
    map.insert(String::from("LIST.SET"), Instruction::new(list_set));
    map.insert(String::from("LIST.BVAL"), Instruction::new(list_bval));
    map.insert(String::from("LIST.IVAL"), Instruction::new(list_ival));
    map.insert(String::from("LIST.FVAL"), Instruction::new(list_fval));
    map.insert(
        String::from("LIST.NEIGHBOR*IDS"),
        Instruction::new(list_neighbor_ids),
    );
    map.insert(
        String::from("LIST.NEIGHBOR*BVALS"),
        Instruction::new(list_neighbor_bvals),
    );
    map.insert(
        String::from("LIST.NEIGHBOR*IVALS"),
        Instruction::new(list_neighbor_ivals),
    );
    map.insert(
        String::from("LIST.NEIGHBOR*FVALS"),
        Instruction::new(list_neighbor_fvals),
    );
}

/// Returns the nth integer that is contained in the item.
/// If no such value exists it returns 0
pub fn bval(item: &Item, n: &usize) -> bool {
    let default = false;
    match Item::find(item, &Item::bool(false), &mut 0, n) {
        Ok(bval) => match bval {
            Item::Literal { push_type } => match push_type {
                PushType::Bool { val } => return val,
                _ => (),
            },
            _ => (),
        },
        Err(_cnt) => (),
    };
    return default;
}

/// Returns the first integer that is contained in the item.
/// If no such value exists it returns 0
pub fn ival(item: &Item, n: &usize) -> i32 {
    let default = 0;
    match Item::find(item, &Item::int(0), &mut 0, n) {
        Ok(ival) => match ival {
            Item::Literal { push_type } => match push_type {
                PushType::Int { val } => return val,
                _ => (),
            },
            _ => (),
        },
        Err(_cnt) => (),
    };
    return default;
}

/// Returns the first float that is contained in the item.
/// If no such value exists it returns 0
pub fn fval(item: &Item, n: &usize) -> f32 {
    let default = 0.0;
    match Item::find(item, &Item::float(0.0), &mut 0, n) {
        Ok(fval) => match fval {
            Item::Literal { push_type } => match push_type {
                PushType::Float { val } => return val,
                _ => (),
            },
            _ => (),
        },
        Err(_cnt) => (),
    };
    return default;
}

/// Generates a vector of items as specified by the top INTVECTOR.
/// Each entry is matched against the stack ids. If there is a match the item
/// of the stack is popped and added to the new list item. As last entry
/// it adds a auto-generated ID.
pub fn load_items(push_state: &mut PushState) -> Option<Vec<Item>> {
    if let Some(stack_ids) = push_state.int_vector_stack.pop() {
        let mut items = vec![];
        for &sid in &stack_ids.values {
            match sid {
                BOOL_STACK_ID => {
                    if let Some(bi) = push_state.bool_stack.pop() {
                        items.push(Item::bool(bi));
                    }
                }
                BOOL_VECTOR_STACK_ID => {
                    if let Some(bvi) = push_state.bool_vector_stack.pop() {
                        items.push(Item::boolvec(bvi));
                    }
                }
                CODE_STACK_ID => {
                    if let Some(ci) = push_state.code_stack.pop() {
                        items.push(ci);
                    }
                }
                EXEC_STACK_ID => {
                    if let Some(ei) = push_state.exec_stack.pop() {
                        items.push(ei);
                    }
                }
                FLOAT_STACK_ID => {
                    if let Some(fi) = push_state.float_stack.pop() {
                        items.push(Item::float(fi));
                    }
                }
                FLOAT_VECTOR_STACK_ID => {
                    if let Some(fvi) = push_state.float_vector_stack.pop() {
                        items.push(Item::floatvec(fvi));
                    }
                }
                INT_STACK_ID => {
                    if let Some(ii) = push_state.int_stack.pop() {
                        items.push(Item::int(ii));
                    }
                }
                INT_VECTOR_STACK_ID => {
                    if let Some(ivi) = push_state.int_vector_stack.pop() {
                        items.push(Item::intvec(ivi));
                    }
                }
                NAME_STACK_ID => {
                    if let Some(ni) = push_state.name_stack.pop() {
                        items.push(Item::name(ni));
                    }
                }
                _ => (),
            }
        }
        return Some(items);
    }
    return None;
}

/// Generates a new list using the items specified on top of the INTVECTOR stack.
pub fn new_list(push_state: &mut PushState) -> Option<Vec<Item>> {
    if let Some(items) = load_items(push_state) {
        // items.reverse();
        return Some(items);
    }
    return None;
}

/// LIST.ADD: Pushes a list item to the code stack with the content
/// specified by the top item of the INTVECTOR. Each entry of the INTVECTOR
/// represents the stack id of an item to be contained.
pub fn list_add(push_state: &mut PushState, _instruction_set: &InstructionCache) {
    if let Some(items) = new_list(push_state) {
        let list_item = Item::list(items);
        push_state.code_stack.push(list_item);
    }
}

/// LIST.REMOVE: Removes the list item at index i.
/// The index i is taken from the top of the INTEGER stack and min-max corrected.
pub fn list_remove(push_state: &mut PushState, _instruction_set: &InstructionCache) {
    if let Some(index) = push_state.int_stack.pop() {
        let size = push_state.code_stack.size() as i32;
        let list_index = i32::max(i32::min(size - 1, index), 0) as usize;
        push_state.code_stack.remove(list_index);
    }
}

/// LIST.GET: Pushes a copy of the items at the given stack position to the execution stack.
/// The first element of the list (the id) is removed.
/// The index i is taken from the top of the INTEGER stack and min-max corrected.
pub fn list_get(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(index) = push_state.int_stack.pop() {
        let size = push_state.code_stack.size() as i32;
        let list_index = i32::max(i32::min(size - 1, index), 0) as usize;
        if let Some(list) = push_state.code_stack.copy(list_index) {
            match list {
                Item::List { items } => {
                    // items.reverse();
                    push_state.exec_stack.push(Item::List { items: items });
                }
                _ => (),
            }
        }
    }
}

/// LIST.BVAL: Pushes the nth BOOLEAN contained in the list item at stack position i.
/// The index i is taken from the top of the INTEGER stack and min-max corrected.
pub fn list_bval(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(index) = push_state.int_stack.pop_vec(2) {
        let size = push_state.code_stack.size() as i32;
        let list_index = i32::max(i32::min(size - 1, index[0]), 0) as usize;
        if let Some(list_item) = push_state.code_stack.get(list_index) {
            push_state
                .bool_stack
                .push(bval(list_item, &(index[1] as usize)));
        }
    }
}

/// LIST.IVAL: Pushes the nth INTEGER contained in the list item at stack position i.
/// The index i is taken from the top of the INTEGER stack and min-max corrected.
pub fn list_ival(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(index) = push_state.int_stack.pop_vec(2) {
        let size = push_state.code_stack.size() as i32;
        let list_index = i32::max(i32::min(size - 1, index[0]), 0) as usize;
        if let Some(list_item) = push_state.code_stack.get(list_index) {
            push_state
                .int_stack
                .push(ival(list_item, &(index[1] as usize)));
        }
    }
}

/// LIST.FVAL: Pushes the nth INTEGER contained in the list item at stack position i.
/// The index i is taken from the top of the INTEGER stack and min-max corrected.
pub fn list_fval(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(index) = push_state.int_stack.pop_vec(2) {
        let size = push_state.code_stack.size() as i32;
        let list_index = i32::max(i32::min(size - 1, index[0]), 0) as usize;
        if let Some(list_item) = push_state.code_stack.get(list_index) {
            push_state
                .float_stack
                .push(fval(list_item, &(index[1] as usize)));
        }
    }
}

/// LIST.SET: Replaces the items bound to the specified index. The list index is taken
/// from the top of the INTEGER stack and min-max corrected. The content is taken from
/// the stacks that are identified by the top INTVECTOR element. For example
/// [ INTEGER.ID INTEGER.ID BOOLEAN.ID ] = [ 9 9 1 ] replaces with a list containing
/// two integer and a boolean item.
pub fn list_set(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(index) = push_state.int_stack.pop() {
        let size = push_state.code_stack.size() as i32;
        let list_index = i32::max(i32::min(size - 1, index), 0) as usize;
        if let Some(items) = load_items(push_state) {
            // items.reverse();
            let list_item = Item::list(items);
            let _res = push_state.code_stack.replace(list_index, list_item);
        }
    }
}

/// LIST.NEIGHBORS*ID: Calculates the neighborhood for a given index element and length. It
/// pushes the indices that are contained in this neighborhood to the INTVECTOR stack.
/// The size, the number of dimensions and index (vector topology) are taken from the INTEGER
/// stack in that order. The radius is taken from the float stack. Distances are calculated using the
/// Eucledian metric. All values are corrected by max-min. If the size of the top element is not a power
/// of the dimensions the smallest hypercube that includes the indices is used to represent the
/// topology, e.g. two dimensions and size = 38 is represented by[7,7]. Neighbor indices that
/// do no exist (e.g. 40) are ignored.
pub fn list_neighbor_ids(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(topology) = push_state.int_stack.pop_vec(3) {
        let size = i32::max(topology[2], 0);
        let index = i32::max(i32::min(size - 1, topology[1]), 0) as usize;
        let dimensions = i32::max(i32::min(size, topology[0]), 0) as usize;
        if let Some(fval) = push_state.float_stack.pop() {
            let radius = f32::max(fval, 0.0);
            if let Some(neighbors) =
                Topology::find_neighbors(&(size as usize), &dimensions, &index, &radius)
            {
                let mut result = vec![];
                for n in neighbors.values.iter() {
                    result.push(*n);
                }
                push_state.int_vector_stack.push(IntVector::new(result));
            }
        }
    }
}

/// LIST.NEIGHBOR*BVALS: Pushes the sorting value of the neighborhood for a given index to the
/// BOOLVECTOR stack. The neighborhood is calculated as in LIST.NEIGHBOR*IDS.
pub fn list_neighbor_bvals(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(topology) = push_state.int_stack.pop_vec(4) {
        let position = topology[3] as usize;
        let size = i32::max(topology[2], 0);
        let index = i32::max(i32::min(size - 1, topology[1]), 0) as usize;
        let dimensions = i32::max(i32::min(size, topology[0]), 0) as usize;
        if let Some(fval) = push_state.float_stack.pop() {
            let radius = f32::max(fval, 0.0);
            if let Some(neighbors) =
                Topology::find_neighbors(&(size as usize), &dimensions, &index, &radius)
            {
                let mut result = vec![];
                for n in neighbors.values.iter() {
                    if let Some(item) = push_state.code_stack.get(*n as usize) {
                        result.push(bval(item, &position));
                    }
                }
                push_state.bool_vector_stack.push(BoolVector::new(result));
            }
        }
    }
}

/// LIST.NEIGHBOR*IVALS: Pushes the sorting value of the neighborhood for a given index to the
/// INTVECTOR stack. The neighborhood is calculated as in LIST.NEIGHBOR*IDS.
pub fn list_neighbor_ivals(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(topology) = push_state.int_stack.pop_vec(4) {
        let position = topology[3] as usize;
        let size = i32::max(topology[2], 0);
        let index = i32::max(i32::min(size - 1, topology[1]), 0) as usize;
        let dimensions = i32::max(i32::min(size, topology[0]), 0) as usize;
        if let Some(fval) = push_state.float_stack.pop() {
            let radius = f32::max(fval, 0.0);
            if let Some(neighbors) =
                Topology::find_neighbors(&(size as usize), &dimensions, &index, &radius)
            {
                let mut result = vec![];
                for n in neighbors.values.iter() {
                    if let Some(item) = push_state.code_stack.get(*n as usize) {
                        result.push(ival(item, &position));
                    }
                }
                push_state.int_vector_stack.push(IntVector::new(result));
            }
        }
    }
}

/// LIST.NEIGHBOR*FVALS: Pushes the sorting value of the neighborhood for a given index to the
/// FLOATVECTOR stack. The neighborhood is calculated as in LIST.NEIGHBOR*IDS.
pub fn list_neighbor_fvals(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
    if let Some(topology) = push_state.int_stack.pop_vec(4) {
        let position = topology[3] as usize;
        let size = i32::max(topology[2], 0);
        let index = i32::max(i32::min(size - 1, topology[1]), 0) as usize;
        let dimensions = i32::max(i32::min(size, topology[0]), 0) as usize;
        if let Some(rval) = push_state.float_stack.pop() {
            let radius = f32::max(rval, 0.0);
            if let Some(neighbors) =
                Topology::find_neighbors(&(size as usize), &dimensions, &index, &radius)
            {
                let mut result = vec![];
                for n in neighbors.values.iter() {
                    if let Some(item) = push_state.code_stack.get(*n as usize) {
                        result.push(fval(item, &position));
                    }
                }
                push_state.float_vector_stack.push(FloatVector::new(result));
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::push::instructions::InstructionSet;
    use crate::push::interpreter::{PushInterpreter, PushInterpreterState};
    use crate::push::vector::*;

    pub fn icache() -> InstructionCache {
        InstructionCache::new(vec![])
    }

    /// Creates a test list entry with the given
    /// value to sort.
    pub fn litem(i: i32) -> Item {
        Item::list(vec![Item::int(i)])
    }

    #[test]
    fn list_add_from_different_stacks() {
        let mut test_state = PushState::new();
        test_state.bool_stack.push(true);
        test_state.int_stack.push(1);
        test_state.float_stack.push(1.0);
        test_state
            .float_vector_stack
            .push(FloatVector::new(vec![3.0]));
        test_state
            .bool_vector_stack
            .push(BoolVector::from_int_array(vec![1]));
        test_state.int_vector_stack.push(IntVector::new(vec![22]));
        test_state.int_vector_stack.push(IntVector::new(vec![
            BOOL_STACK_ID,
            FLOAT_STACK_ID,
            FLOAT_VECTOR_STACK_ID,
            BOOL_VECTOR_STACK_ID,
            INT_VECTOR_STACK_ID,
            INT_STACK_ID,
        ]));
        list_add(&mut test_state, &icache());
        assert_eq!(test_state.code_stack.to_string(), "( 1 [22] [TRUE] [3.000] 1.000 TRUE )");
    }

    #[test]
    fn list_remove_code_items() {
        let mut test_state = PushState::new();
        test_state.code_stack.push(Item::int(1));
        test_state.code_stack.push(Item::list(vec![
            Item::bool(true),
            Item::int(2),
            Item::int(3),
            Item::float(2.3),
        ]));
        test_state.code_stack.push(Item::int(2));
        test_state.int_stack.push(1);
        list_remove(&mut test_state, &icache());
        assert_eq!(
            test_state.code_stack.to_string(),
            "2 1"
        );
    }

    #[test]
    fn list_get_pushes_code_items() {
        let mut test_state = PushState::new();
        test_state.code_stack.push(Item::int(1));
        test_state.code_stack.push(Item::list(vec![
            Item::bool(true),
            Item::int(2),
            Item::int(3),
            Item::float(2.3),
        ]));
        test_state.code_stack.push(Item::int(2));
        test_state.int_stack.push(1);
        list_get(&mut test_state, &icache());
        assert_eq!(
            test_state.exec_stack.to_string(),
            "( 2.300 3 2 TRUE )",
            "Order of elements should be reversed"
        );
    }

    #[test]
    fn list_bval_returns_first_int_element() {
        let mut test_state = PushState::new();
        test_state
            .code_stack
            .push(Item::list(vec![Item::bool(true), Item::int(2)]));
        test_state.int_stack.push(0); // Stack Position
        test_state.int_stack.push(0); // Item Position
        list_bval(&mut test_state, &icache());
        assert_eq!(test_state.bool_stack.pop().unwrap(), true);
    }

    #[test]
    fn list_ival_returns_first_int_element() {
        let mut test_state = PushState::new();
        test_state
            .code_stack
            .push(Item::list(vec![Item::int(1), Item::int(2)]));
        test_state.int_stack.push(0); // Stack Position
        test_state.int_stack.push(0); // Item Position
        list_ival(&mut test_state, &icache());
        assert_eq!(test_state.int_stack.pop().unwrap(), 2);
    }

    #[test]
    fn list_fval_returns_first_float_element() {
        let mut test_state = PushState::new();
        test_state
            .code_stack
            .push(Item::list(vec![Item::float(1.0), Item::int(2)]));
        test_state.int_stack.push(0); // Stack Position
        test_state.int_stack.push(0); // Item Position
        list_fval(&mut test_state, &icache());
        assert_eq!(test_state.float_stack.pop().unwrap(), 1.0);
    }

    #[test]
    fn list_set_replaces_code_item() {
        let mut test_state = PushState::new();
        test_state.bool_stack.push(true);
        test_state.bool_stack.push(false);
        test_state
            .int_vector_stack
            .push(IntVector::new(vec![BOOL_STACK_ID, BOOL_STACK_ID]));
        test_state.int_stack.push(1);
        test_state.code_stack.push(Item::int(11));
        test_state.code_stack.push(Item::int(22));
        test_state.code_stack.push(Item::int(33));
        list_set(&mut test_state, &icache());
        assert_eq!(
            test_state.code_stack.to_string(),
            "33 ( TRUE FALSE ) 11",
            "Order of new list element reversed"
        );
    }

    #[test]
    fn list_add_get_preserves_stack_positions() {
        let mut test_state = PushState::new();
        test_state.bool_stack.push(true);
        test_state.bool_stack.push(false);
        assert_eq!(
            test_state.bool_stack.to_string(),
            "FALSE TRUE",
            "Initial order of items"
        );
        test_state
            .int_vector_stack
            .push(IntVector::new(vec![BOOL_STACK_ID, BOOL_STACK_ID]));
        list_add(&mut test_state, &icache());
        test_state.int_stack.push(0);
        list_get(&mut test_state, &icache());
        // Run execution
        let mut instruction_set = InstructionSet::new();
        instruction_set.load();
        assert_eq!(
            PushInterpreter::run(&mut test_state, &mut instruction_set),
            PushInterpreterState::NoErrors
        );
        assert_eq!(
            test_state.bool_stack.to_string(),
            "FALSE TRUE",
            "Push/Pull of list preserves order of items"
        );
    }
    #[test]
    fn list_set_get_preserves_stack_positions() {
        let mut test_state = PushState::new();
        test_state.bool_stack.push(true);
        test_state.bool_stack.push(false);
        assert_eq!(
            test_state.bool_stack.to_string(),
            "FALSE TRUE",
            "Initial order of items"
        );
        test_state
            .int_vector_stack
            .push(IntVector::new(vec![BOOL_STACK_ID, BOOL_STACK_ID]));
        test_state.int_stack.push(1);
        test_state.code_stack.push(Item::int(11));
        test_state.code_stack.push(Item::int(22));
        test_state.code_stack.push(Item::int(33));
        list_set(&mut test_state, &icache());
        test_state.int_stack.push(1);
        list_get(&mut test_state, &icache());
        // Run execution
        let mut instruction_set = InstructionSet::new();
        instruction_set.load();
        assert_eq!(
            PushInterpreter::run(&mut test_state, &mut instruction_set),
            PushInterpreterState::NoErrors
        );
        assert_eq!(
            test_state.bool_stack.to_string(),
            "FALSE TRUE",
            "Push/Pull of list preserves order of items"
        );
    }

    #[test]
    fn list_neighbor_ids_pushes_result_for_valid_index() {
        let mut test_state = PushState::new();
        test_state.float_stack.push(1.5); // Radius
        test_state.int_stack.push(2); // Dimensions
        test_state.int_stack.push(50); // Index
        test_state.int_stack.push(100); // Size
        list_neighbor_ids(&mut test_state, &icache());
        assert_eq!(
            test_state.int_vector_stack.to_string(),
            String::from("[40,41,50,51,60,61]")
        );
    }

    #[test]
    fn list_neighbor_ids_corrects_out_of_bounds_index() {
        let mut test_state = PushState::new();
        test_state.float_stack.push(1.5); // Radius
        test_state.int_stack.push(2); // Dimensions
        test_state.int_stack.push(105); // Index
        test_state.int_stack.push(100); // Size
        list_neighbor_ids(&mut test_state, &icache());
        assert_eq!(
            test_state.int_vector_stack.to_string(),
            String::from("[88,89,98,99]")
        );
        test_state.int_vector_stack.flush();
        test_state.float_stack.push(1.5); // Radius
        test_state.int_stack.push(2); // Dimensions
        test_state.int_stack.push(-10); // Index
        test_state.int_stack.push(100); // Size
        list_neighbor_ids(&mut test_state, &icache());
        assert_eq!(
            test_state.int_vector_stack.to_string(),
            String::from("[0,1,10,11]")
        );
    }

    #[test]
    fn list_neighbor_ivals_pushes_sort_values() {
        let mut test_state = PushState::new();
        test_state.float_stack.push(1.0); // Radius
        test_state.int_stack.push(2); // Dimensions
        test_state.int_stack.push(0); // Index
        test_state.int_stack.push(9); // Size
        for i in 10..20 {
            test_state.code_stack.push(litem(i));
        }
        list_neighbor_ids(&mut test_state, &icache());
        assert_eq!(
            test_state.int_vector_stack.to_string(),
            String::from("[0,1,3]")
        );
        test_state.int_vector_stack.flush();
        test_state.float_stack.push(1.0); // Radius
        test_state.int_stack.push(2); // Dimensions
        test_state.int_stack.push(0); // Index
        test_state.int_stack.push(9); // Size
        test_state.int_stack.push(0); // Position
        list_neighbor_ivals(&mut test_state, &icache());
        assert_eq!(
            test_state.int_vector_stack.to_string(),
            String::from("[19,18,16]")
        );
    }
}