-
Notifications
You must be signed in to change notification settings - Fork 4
/
arrays.rs
120 lines (88 loc) · 3.88 KB
/
arrays.rs
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
#![allow(clippy::restriction)]
use libpd_rs::functions::{
array::{
array_size, read_double_array_from, read_float_array_from, resize_array,
write_double_array_to, write_float_array_to,
},
close_patch,
};
#[test]
fn apply_array_operations_in_a_row() {
libpd_rs::functions::init().unwrap();
let handle = libpd_rs::functions::open_patch("tests/patches/array_sketch_pad.pd").unwrap();
let bad_name = "not_exists";
let sketch_pad = "sketch_pad";
assert!(array_size(bad_name).is_err());
// Default
let size = array_size(sketch_pad).unwrap();
assert_eq!(size, 100);
assert!(resize_array(bad_name, 1024).is_err());
resize_array(sketch_pad, -1).unwrap();
let size = array_size(sketch_pad).unwrap();
assert_eq!(size, 1);
resize_array(sketch_pad, 0).unwrap();
let size = array_size(sketch_pad).unwrap();
assert_eq!(size, 1);
// Protected with size limits. Over the limit is also 1.
resize_array(sketch_pad, i32::MAX).unwrap();
let size = array_size(sketch_pad).unwrap();
assert_eq!(size, 1);
resize_array(sketch_pad, 1024).unwrap();
let size = array_size(sketch_pad).unwrap();
assert_eq!(size, 1024);
resize_array(sketch_pad, 8).unwrap();
// Reading and writing works floats.
let will_write_f: Vec<f32> = vec![0.0, 0.0, 1.0, 1.0];
let mut read_to: Vec<f32> = vec![0.0; 4];
write_float_array_to(sketch_pad, 0, &will_write_f, 4).unwrap();
read_float_array_from(sketch_pad, 0, 4, &mut read_to).unwrap();
assert_eq!(read_to, will_write_f);
let will_write_d: Vec<f64> = vec![0.0, 0.0, 1.0, 1.0];
let mut read_to: Vec<f64> = vec![0.0; 4];
write_double_array_to(sketch_pad, 0, &will_write_d, 4).unwrap();
read_double_array_from(sketch_pad, 0, 4, &mut read_to).unwrap();
assert_eq!(read_to, will_write_d);
//
// Clear float arrays.
let mut read_to: Vec<f32> = vec![0.0; 6];
write_float_array_to(sketch_pad, 0, &[0.0, 0.0, 0.0, 0.0], 4).unwrap();
// Offsets
write_float_array_to(sketch_pad, 2, &[1.0, 1.0, 0.0, 0.0], 4).unwrap();
read_float_array_from(sketch_pad, 0, 6, &mut read_to).unwrap();
assert_eq!(read_to, vec![0.0, 0.0, 1.0, 1.0, 0.0, 0.0]);
// Clear float arrays.
let mut read_to: Vec<f64> = vec![0.0; 6];
write_double_array_to(sketch_pad, 0, &[0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 6).unwrap();
// Offsets
write_double_array_to(sketch_pad, 2, &[1.0, 1.0, 0.0, 0.0], 4).unwrap();
read_double_array_from(sketch_pad, 0, 6, &mut read_to).unwrap();
assert_eq!(read_to, vec![0.0, 0.0, 1.0, 1.0, 0.0, 0.0]);
// Bounds
// Double
let will_write_long_array: Vec<f64> = vec![0.0; 1024];
let result = write_double_array_to(sketch_pad, 4, &will_write_long_array, 1024);
assert!(result.is_err());
let mut read_to: Vec<f64> = vec![0.0; 4];
let result = read_double_array_from(sketch_pad, 0, 50, &mut read_to);
assert!(result.is_err());
let mut read_to: Vec<f64> = vec![0.0; 1024];
let result = read_double_array_from(sketch_pad, 0, 1024, &mut read_to);
assert!(result.is_err());
let mut read_to: Vec<f64> = vec![0.0; 1024];
let result = read_double_array_from(sketch_pad, 0, -1, &mut read_to);
assert!(result.is_err());
// Float
let will_write_long_array: Vec<f32> = vec![0.0; 1024];
let result = write_float_array_to(sketch_pad, 4, &will_write_long_array, 1024);
assert!(result.is_err());
let mut read_to: Vec<f32> = vec![0.0; 4];
let result = read_float_array_from(sketch_pad, 0, 50, &mut read_to);
assert!(result.is_err());
let mut read_to: Vec<f32> = vec![0.0; 1024];
let result = read_float_array_from(sketch_pad, 0, 1024, &mut read_to);
assert!(result.is_err());
let mut read_to: Vec<f32> = vec![0.0; 1024];
let result = read_float_array_from(sketch_pad, 0, -1, &mut read_to);
assert!(result.is_err());
close_patch(handle).unwrap();
}