File size: 7,306 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
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
{ SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
   Licensing: see LICENSE in the repository root. }

{ Test Suite: Comprehensive conformance tests for NraayTensor reference implementation.
  Tests core operations, governance properties, and state machine transitions.
}

unit tests;

{$MODE FPC}

interface

uses
  descriptor, buffer, tensor_array, ownership, governance, materialize, tensor, validation;

type
  { Test statistics }
  TTestStats = record
    total_tests: integer;
    passed: integer;
    failed: integer;
  end;

{ Run all conformance tests }
function RunAllTests: TTestStats;

{ Test: Tensor allocation }
function TestAllocation: boolean;

{ Test: Element get/set }
function TestElementAccess: boolean;

{ Test: Clone creates shared reference }
function TestClone: boolean;

{ Test: Slicing creates non-contiguous view }
function TestSlicing: boolean;

{ Test: Transpose permutes axes }
function TestTranspose: boolean;

{ Test: Materialization restores contiguity }
function TestMaterialization: boolean;

{ Test: Generation tracking }
function TestGenerationTracking: boolean;

{ Test: Governance lemmas }
function TestGovernanceLemmas: boolean;

{ Test: Buffer reference counting }
function TestRefCounting: boolean;

{ Test: Composite workflow }
function TestCompositeWorkflow: boolean;

implementation

var
  test_stats: TTestStats;

function TestAllocation: boolean;
var
  t: TNraayTensor;
  shape: array [0..1] of uint32;
begin
  shape[0] := 3;
  shape[1] := 4;

  t := CreateTensor(shape);
  result := IsValid(t);

  if result then
  begin
    result := (Length(t) = 12) and (Rank(t) = 2);
  end;

  DestroyTensor(t);
end;

function TestElementAccess: boolean;
var
  t: TNraayTensor;
  shape: array [0..1] of uint32;
  coord: array [0..1] of uint32;
begin
  shape[0] := 2;
  shape[1] := 3;

  t := CreateTensor(shape);

  coord[0] := 1;
  coord[1] := 2;
  SetElement(t, coord, 42.0);

  result := (GetElement(t, coord) = 42.0);

  DestroyTensor(t);
end;

function TestClone: boolean;
var
  t1, t2: TNraayTensor;
  shape: array [0..1] of uint32;
begin
  shape[0] := 2;
  shape[1] := 2;

  t1 := CreateTensor(shape);
  t2 := CloneTensor(t1);

  { Both should share same buffer }
  result := (GetDataPtr(t1.data) = GetDataPtr(t2.data));

  { Refcount should be 2 }
  if result then
    result := (GetRefCount(t2.data) = 2);

  DestroyTensor(t1);
  DestroyTensor(t2);
end;

function TestSlicing: boolean;
var
  t: TNraayTensor;
  sliced: TNraayTensor;
  shape: array [0..1] of uint32;
  ranges: array [0..1] of array of uint32;
begin
  shape[0] := 5;
  shape[1] := 5;

  t := CreateTensor(shape);

  { Create dummy ranges for slicing }
  setlength(ranges[0], 3);
  setlength(ranges[1], 3);
  ranges[0][0] := 1;
  ranges[0][1] := 4;
  ranges[0][2] := 1;
  ranges[1][0] := 1;
  ranges[1][1] := 4;
  ranges[1][2] := 1;

  sliced := Slice(t, ranges);

  { Sliced should not be contiguous }
  result := (sliced.descriptor.is_contiguous = 0);

  { Should be in SHARED state }
  if result then
    result := (sliced.descriptor.owns_storage = 0);

  DestroyTensor(t);
  DestroyTensor(sliced);
end;

function TestTranspose: boolean;
var
  t: TNraayTensor;
  transposed: TNraayTensor;
  shape: array [0..1] of uint32;
  perm: array [0..1] of uint32;
begin
  shape[0] := 3;
  shape[1] := 4;

  t := CreateTensor(shape);

  perm[0] := 1;
  perm[1] := 0;

  transposed := Transpose(t, perm);

  { Shape should be swapped }
  result := (transposed.descriptor.shape_ptr^[0] = 4) and
            (transposed.descriptor.shape_ptr^[1] = 3);

  DestroyTensor(t);
  DestroyTensor(transposed);
end;

function TestMaterialization: boolean;
var
  t: TNraayTensor;
  shape: array [0..1] of uint32;
begin
  shape[0] := 2;
  shape[1] := 2;

  t := CreateTensor(shape);

  { Should already be materialized }
  result := (t.descriptor.is_contiguous = 1) and
            (t.descriptor.owns_storage = 1);

  Materialize(t);

  result := result and
            (t.descriptor.is_contiguous = 1) and
            (t.descriptor.owns_storage = 1) and
            (t.descriptor.offset = 0);

  DestroyTensor(t);
end;

function TestGenerationTracking: boolean;
var
  t: TNraayTensor;
  shape: array [0..0] of uint32;
  gen1, gen2: uint32;
begin
  shape[0] := 5;

  t := CreateTensor(shape);
  gen1 := t.descriptor.generation;

  result := (gen1 >= 0);

  DestroyTensor(t);
end;

function TestGovernanceLemmas: boolean;
var
  t: TNraayTensor;
  shape: array [0..1] of uint32;
begin
  shape[0] := 2;
  shape[1] := 3;

  t := CreateTensor(shape);

  { All lemmas should verify }
  result := VerifyGovernanceLemmas(t, t.data.buffer);

  DestroyTensor(t);
end;

function TestRefCounting: boolean;
var
  t1, t2, t3: TNraayTensor;
  shape: array [0..0] of uint32;
  count: uint32;
begin
  shape[0] := 10;

  t1 := CreateTensor(shape);
  count := GetRefCount(t1.data);
  result := (count = 1);

  t2 := CloneTensor(t1);
  count := GetRefCount(t2.data);
  result := result and (count = 2);

  t3 := CloneTensor(t2);
  count := GetRefCount(t3.data);
  result := result and (count = 3);

  DestroyTensor(t1);
  DestroyTensor(t2);
  DestroyTensor(t3);
end;

function TestCompositeWorkflow: boolean;
var
  t1, t2, sliced, materialized: TNraayTensor;
  shape: array [0..1] of uint32;
  ranges: array [0..1] of array of uint32;
  coord: array [0..1] of uint32;
begin
  shape[0] := 4;
  shape[1] := 4;

  t1 := CreateTensor(shape);
  result := IsValid(t1);

  { Set a value }
  coord[0] := 2;
  coord[1] := 2;
  SetElement(t1, coord, 3.14);
  result := result and (GetElement(t1, coord) = 3.14);

  { Clone }
  t2 := CloneTensor(t1);
  result := result and (GetRefCount(t2.data) = 2);

  { Materialize }
  Materialize(t2);
  result := result and (t2.descriptor.is_contiguous = 1);

  DestroyTensor(t1);
  DestroyTensor(t2);
end;

function RunAllTests: TTestStats;
begin
  with result do
  begin
    total_tests := 0;
    passed := 0;
    failed := 0;
  end;

  { Test allocation }
  inc(result.total_tests);
  if TestAllocation then
    inc(result.passed)
  else
    inc(result.failed);

  { Test element access }
  inc(result.total_tests);
  if TestElementAccess then
    inc(result.passed)
  else
    inc(result.failed);

  { Test cloning }
  inc(result.total_tests);
  if TestClone then
    inc(result.passed)
  else
    inc(result.failed);

  { Test slicing }
  inc(result.total_tests);
  if TestSlicing then
    inc(result.passed)
  else
    inc(result.failed);

  { Test transpose }
  inc(result.total_tests);
  if TestTranspose then
    inc(result.passed)
  else
    inc(result.failed);

  { Test materialization }
  inc(result.total_tests);
  if TestMaterialization then
    inc(result.passed)
  else
    inc(result.failed);

  { Test generation tracking }
  inc(result.total_tests);
  if TestGenerationTracking then
    inc(result.passed)
  else
    inc(result.failed);

  { Test governance lemmas }
  inc(result.total_tests);
  if TestGovernanceLemmas then
    inc(result.passed)
  else
    inc(result.failed);

  { Test reference counting }
  inc(result.total_tests);
  if TestRefCounting then
    inc(result.passed)
  else
    inc(result.failed);

  { Test composite workflow }
  inc(result.total_tests);
  if TestCompositeWorkflow then
    inc(result.passed)
  else
    inc(result.failed);
end;

end.