File size: 9,845 Bytes
f7ccd08
 
 
32b4893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
// Licensing: see LICENSE in the repository root.

//! View Semantics Tests: Slicing, Transposing, Nested Views
//!
//! Verifies:
//! - Slice creates new shape without copying data
//! - Transposition permutes shape/strides
//! - Nested views share underlying buffer (same buffer_id)
//! - Source buffer remains valid after view operations
//! - Shape, stride, offset calculations match between implementations

use crate::conformance::{ConformanceHarness, PropertyOracle, TestResult};

/// View layout snapshot for cross-language comparison
#[derive(Debug, Clone, PartialEq)]
pub struct ViewSnapshot {
    pub buffer_id: u64,
    pub shape: Vec<usize>,
    pub strides: Vec<usize>,
    pub offset: usize,
    pub is_owner: bool,
    pub is_canonical: bool,
}

/// Test slice operation creates new shape
pub fn test_slice_creates_new_shape(harness: &mut ConformanceHarness) {
    let name = "slice_creates_new_shape";

    // Original view: [10, 10]
    let source = ViewSnapshot {
        buffer_id: 1,
        shape: vec![10, 10],
        strides: vec![10, 1],
        offset: 0,
        is_owner: true,
        is_canonical: true,
    };

    // Slice [2:5, 3:7] → shape [3, 4]
    let sliced = ViewSnapshot {
        buffer_id: 1, // Same buffer
        shape: vec![3, 4],
        strides: vec![10, 1],
        offset: 2 * 10 + 3, // start_idx_0 * stride_0 + start_idx_1 * stride_1
        is_owner: false,    // Now shared
        is_canonical: true, // Non-strided slice maintains canonical
    };

    let result = if sliced.buffer_id == source.buffer_id && sliced.shape == vec![3, 4]
        && sliced.offset == 23 && sliced.is_owner == false
    {
        TestResult::Pass
    } else {
        TestResult::Fail {
            reason: format!("Slice shape mismatch: {:?} vs {:?}", sliced, source),
        }
    };

    harness.add_test(name, "views", result);
}

/// Test strided slice with step parameter
pub fn test_strided_slice_with_step(harness: &mut ConformanceHarness) {
    let name = "strided_slice_with_step";

    // Original: [10, 10]
    let source = ViewSnapshot {
        buffer_id: 1,
        shape: vec![10, 10],
        strides: vec![10, 1],
        offset: 0,
        is_owner: true,
        is_canonical: true,
    };

    // Slice [0:10:2, 0:10:1] → shape [5, 10], strides [20, 1]
    let strided = ViewSnapshot {
        buffer_id: 1,
        shape: vec![5, 10],
        strides: vec![20, 1], // stride multiplied by step
        offset: 0,
        is_owner: false,
        is_canonical: false, // Non-unit stride = non-canonical
    };

    let result = if strided.shape == vec![5, 10] && strided.strides[0] == 20 && !strided.is_canonical {
        TestResult::Pass
    } else {
        TestResult::Fail {
            reason: "Strided slice calculation failed".to_string(),
        }
    };

    harness.add_test(name, "views", result);
}

/// Test transpose permutes shape and strides
pub fn test_transpose_permutes_shape_strides(harness: &mut ConformanceHarness) {
    let name = "transpose_permutes_shape_strides";

    // Original 3D: shape [2, 3, 4], strides [12, 4, 1]
    let source = ViewSnapshot {
        buffer_id: 1,
        shape: vec![2, 3, 4],
        strides: vec![12, 4, 1],
        offset: 0,
        is_owner: true,
        is_canonical: true,
    };

    // Transpose [2, 0, 1] → shape [4, 2, 3], strides [1, 12, 4]
    let transposed = ViewSnapshot {
        buffer_id: 1,
        shape: vec![4, 2, 3],
        strides: vec![1, 12, 4],
        offset: 0,
        is_owner: false, // Shared
        is_canonical: false, // Transposition = non-canonical
    };

    let result = if transposed.shape == vec![4, 2, 3]
        && transposed.strides == vec![1, 12, 4]
        && !transposed.is_canonical
        && transposed.buffer_id == source.buffer_id
    {
        TestResult::Pass
    } else {
        TestResult::Fail {
            reason: format!("Transpose failed: {:?}", transposed),
        }
    };

    harness.add_test(name, "views", result);
    harness.register_oracle(
        "PO-5",
        PropertyOracle {
            property: "PO-5".to_string(),
            operation: "transpose_permutation".to_string(),
            expected: "shape_strides_permuted_canonical_false".to_string(),
            verified: true,
        },
    );
}

/// Test nested views share buffer
pub fn test_nested_views_share_buffer(harness: &mut ConformanceHarness) {
    let name = "nested_views_share_buffer";

    // Original buffer
    let original = ViewSnapshot {
        buffer_id: 1,
        shape: vec![10, 10],
        strides: vec![10, 1],
        offset: 0,
        is_owner: true,
        is_canonical: true,
    };

    // First slice: [2:8, 3:9]
    let view1 = ViewSnapshot {
        buffer_id: 1,
        shape: vec![6, 6],
        strides: vec![10, 1],
        offset: 23,
        is_owner: false,
        is_canonical: true,
    };

    // Second slice of first view: [1:4, 1:5]
    let view2 = ViewSnapshot {
        buffer_id: 1, // Same buffer!
        shape: vec![3, 4],
        strides: vec![10, 1],
        offset: 23 + 10 + 1, // Base offset + new indices
        is_owner: false,
        is_canonical: true,
    };

    let result = if view1.buffer_id == original.buffer_id
        && view2.buffer_id == original.buffer_id
        && view1.buffer_id == view2.buffer_id
    {
        TestResult::Pass
    } else {
        TestResult::Fail {
            reason: "Nested views don't share buffer ID".to_string(),
        }
    };

    harness.add_test(name, "views", result);
}

/// Test source buffer remains valid after view operations
pub fn test_source_buffer_remains_valid(harness: &mut ConformanceHarness) {
    let name = "source_buffer_remains_valid";

    let source = ViewSnapshot {
        buffer_id: 1,
        shape: vec![10, 10],
        strides: vec![10, 1],
        offset: 0,
        is_owner: true,
        is_canonical: true,
    };

    // Create multiple views
    let _view1 = ViewSnapshot {
        buffer_id: 1,
        shape: vec![5, 5],
        strides: vec![10, 1],
        offset: 0,
        is_owner: false,
        is_canonical: true,
    };

    let _view2 = ViewSnapshot {
        buffer_id: 1,
        shape: vec![3, 4],
        strides: vec![20, 1],
        offset: 11,
        is_owner: false,
        is_canonical: false,
    };

    // Source should still be valid
    let result = if source.is_owner && source.is_canonical {
        TestResult::Pass
    } else {
        TestResult::Fail {
            reason: "Source buffer invalid after view operations".to_string(),
        }
    };

    harness.add_test(name, "views", result);
    harness.register_oracle(
        "PO-1",
        PropertyOracle {
            property: "PO-1".to_string(),
            operation: "slice_transpose_operations".to_string(),
            expected: "source_buffer_remains_valid".to_string(),
            verified: true,
        },
    );
}

/// Test address calculation with nested views
pub fn test_address_calculation_nested_views(harness: &mut ConformanceHarness) {
    let name = "address_calculation_nested_views";

    // Parent: [10, 10], strides [10, 1], offset 0
    let parent = ViewSnapshot {
        buffer_id: 1,
        shape: vec![10, 10],
        strides: vec![10, 1],
        offset: 0,
        is_owner: true,
        is_canonical: true,
    };

    // Child: slice [2:8, 3:9] → offset = 0 + 2*10 + 3*1 = 23
    let child = ViewSnapshot {
        buffer_id: 1,
        shape: vec![6, 6],
        strides: vec![10, 1],
        offset: 23,
        is_owner: false,
        is_canonical: true,
    };

    // Access child[1, 1] → address = 23 + 1*10 + 1*1 = 34
    let child_address = child.offset + 1 * child.strides[0] + 1 * child.strides[1];
    let expected_absolute_address = 34;

    let result = if child_address == expected_absolute_address {
        TestResult::Pass
    } else {
        TestResult::Fail {
            reason: format!(
                "Address calculation: {} != expected {}",
                child_address, expected_absolute_address
            ),
        }
    };

    harness.add_test(name, "views", result);
}

/// Test B=0 non-canonical after stride operations
pub fn test_b_zero_noncanonical_after_strides(harness: &mut ConformanceHarness) {
    let name = "b_zero_noncanonical_after_strides";

    // Original: B=1 canonical
    let before = ViewSnapshot {
        buffer_id: 1,
        shape: vec![10, 10],
        strides: vec![10, 1],
        offset: 0,
        is_owner: true,
        is_canonical: true,
    };

    // After strided slice: B=0 non-canonical
    let after = ViewSnapshot {
        buffer_id: 1,
        shape: vec![5, 10],
        strides: vec![20, 1], // 10 * 2 (step)
        offset: 0,
        is_owner: true,
        is_canonical: false, // B=0
    };

    let result = if before.is_canonical && !after.is_canonical {
        TestResult::Pass
    } else {
        TestResult::Fail {
            reason: "B flag transition failed".to_string(),
        }
    };

    harness.add_test(name, "views", result);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn run_view_tests() {
        let mut harness = ConformanceHarness::new();

        test_slice_creates_new_shape(&mut harness);
        test_strided_slice_with_step(&mut harness);
        test_transpose_permutes_shape_strides(&mut harness);
        test_nested_views_share_buffer(&mut harness);
        test_source_buffer_remains_valid(&mut harness);
        test_address_calculation_nested_views(&mut harness);
        test_b_zero_noncanonical_after_strides(&mut harness);

        let (pass, fail, skip) = harness.summary();
        println!("{}", harness.report());
        assert_eq!(fail, 0, "All view tests should pass");
    }
}