summaryrefslogtreecommitdiff
path: root/morphology-img/tests/sequence_conversion.rs
blob: 30cd49476184cf8fa49759a79b642a5e069e65aa (plain)
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
use morphology::{Sequence, Value};
use morphology_img::note::sequence_to_notes;

#[test]
fn test_example_from_preliminary_overview() {
    // Example from preliminary overview: [(1,0), (-1,1), (-1,0), (0,0)]
    // Length = 4
    // Expected integers: [4, -3, -4, 0]
    // Expected pitches: ["f'", "f", "e", "b"]
    // Expected notes: ["f'4", "f4", "e4", "b4"]

    let seq = Sequence::new(vec![
        Value::new(1, 0),
        Value::new(-1, 1),
        Value::new(-1, 0),
        Value::new(0, 0),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 4);
    assert_eq!(notes[0].as_str(), "f'4");
    assert_eq!(notes[1].as_str(), "f4");
    assert_eq!(notes[2].as_str(), "e4");
    assert_eq!(notes[3].as_str(), "b4");
}

#[test]
fn test_single_element_sequence() {
    // Single origin value
    let seq = Sequence::new(vec![Value::origin()]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 1);
    // (0 * 1) + 0 = 0 -> "b"
    assert_eq!(notes[0].as_str(), "b4");
}

#[test]
fn test_sequence_with_all_zeros() {
    // All origin values
    let seq = Sequence::new(vec![
        Value::origin(),
        Value::origin(),
        Value::origin(),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 3);
    // All should be (0 * 3) + 0 = 0 -> "b"
    assert_eq!(notes[0].as_str(), "b4");
    assert_eq!(notes[1].as_str(), "b4");
    assert_eq!(notes[2].as_str(), "b4");
}

#[test]
fn test_sequence_with_positive_outer() {
    // Sequence with positive outer values
    let seq = Sequence::new(vec![
        Value::new(1, 0),
        Value::new(0, 0),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 2);
    // (1 * 2) + 0 = 2 -> "d'"
    assert_eq!(notes[0].as_str(), "d'4");
    // (0 * 2) + 0 = 0 -> "b"
    assert_eq!(notes[1].as_str(), "b4");
}

#[test]
fn test_sequence_with_negative_outer() {
    // Sequence with negative outer values
    let seq = Sequence::new(vec![
        Value::new(-1, 0),
        Value::new(0, 0),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 2);
    // (-1 * 2) + 0 = -2 -> "g"
    assert_eq!(notes[0].as_str(), "g4");
    // (0 * 2) + 0 = 0 -> "b"
    assert_eq!(notes[1].as_str(), "b4");
}

#[test]
fn test_sequence_with_inner_values() {
    // Sequence with various inner values
    let seq = Sequence::new(vec![
        Value::new(0, 1),
        Value::new(0, -1),
        Value::new(0, 0),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 3);
    // (0 * 3) + 1 = 1 -> "c'"
    assert_eq!(notes[0].as_str(), "c'4");
    // (0 * 3) + (-1) = -1 -> "a"
    assert_eq!(notes[1].as_str(), "a4");
    // (0 * 3) + 0 = 0 -> "b"
    assert_eq!(notes[2].as_str(), "b4");
}

#[test]
fn test_sequence_with_mixed_values() {
    // Sequence with mix of outer and inner values
    let seq = Sequence::new(vec![
        Value::new(2, 3),
        Value::new(-1, -2),
        Value::new(1, 1),
        Value::new(0, 0),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 4);
    // (2 * 4) + 3 = 11 -> "f''"
    assert_eq!(notes[0].as_str(), "f''4");
    // (-1 * 4) + (-2) = -6 -> "c"
    assert_eq!(notes[1].as_str(), "c4");
    // (1 * 4) + 1 = 5 -> "g'"
    assert_eq!(notes[2].as_str(), "g'4");
    // (0 * 4) + 0 = 0 -> "b"
    assert_eq!(notes[3].as_str(), "b4");
}

#[test]
fn test_sequence_length_two() {
    // Test a simple length-2 sequence
    let seq = Sequence::new(vec![
        Value::new(1, 0),
        Value::new(0, 0),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 2);
    assert_eq!(notes[0].as_str(), "d'4");
    assert_eq!(notes[1].as_str(), "b4");
}

#[test]
fn test_length_affects_pitch_calculation() {
    // Test that sequence length affects the pitch calculation
    // Value (1, 0) with length 2 vs length 4 should give different pitches

    let seq2 = Sequence::new(vec![
        Value::new(1, 0),
        Value::new(0, 0),
    ]);
    let notes2 = sequence_to_notes(&seq2);
    // (1 * 2) + 0 = 2
    assert_eq!(notes2[0].as_str(), "d'4");

    let seq4 = Sequence::new(vec![
        Value::new(1, 0),
        Value::new(0, 0),
        Value::new(0, 0),
        Value::new(0, 0),
    ]);
    let notes4 = sequence_to_notes(&seq4);
    // (1 * 4) + 0 = 4
    assert_eq!(notes4[0].as_str(), "f'4");
}

#[test]
fn test_extreme_values() {
    // Test with extreme positive and negative values
    let seq = Sequence::new(vec![
        Value::new(10, 5),
        Value::new(-10, -5),
        Value::new(0, 0),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 3);
    // (10 * 3) + 5 = 35 -> "b'''''"
    assert_eq!(notes[0].as_str(), "b'''''4");
    // (-10 * 3) + (-5) = -35 -> "b,,,,"
    assert_eq!(notes[1].as_str(), "b,,,,,4");
    // (0 * 3) + 0 = 0 -> "b"
    assert_eq!(notes[2].as_str(), "b4");
}

#[test]
fn test_empty_sequence() {
    // Edge case: empty sequence
    let seq = Sequence::new(vec![]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 0);
}

#[test]
fn test_notes_preserve_sequence_order() {
    // Verify that notes maintain the same order as input sequence
    let seq = Sequence::new(vec![
        Value::new(3, 1),
        Value::new(2, 2),
        Value::new(1, 3),
        Value::new(0, 4),
        Value::new(-1, 5),
        Value::new(0, 0),
    ]);

    let notes = sequence_to_notes(&seq);

    assert_eq!(notes.len(), 6);
    // Verify they're all different (order matters)
    for i in 0..notes.len() {
        for j in (i+1)..notes.len() {
            // Most should be different, though some might coincidentally match
            // The important thing is the order is preserved
            let note_i = notes[i].as_str();
            let note_j = notes[j].as_str();
            // Just verify we got valid notes for all positions
            assert!(note_i.ends_with('4'));
            assert!(note_j.ends_with('4'));
        }
    }
}