-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmem_test.go
More file actions
798 lines (724 loc) · 20.7 KB
/
Copy pathmem_test.go
File metadata and controls
798 lines (724 loc) · 20.7 KB
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
package qjs_test
import (
"fmt"
"math"
"testing"
"github.com/fastschema/qjs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// allocateMemoryPtr allocates memory and returns a uint32 pointer
func allocateMemoryPtr(t *testing.T, runtime *qjs.Runtime, size uint64) uint32 {
ptr := runtime.Malloc(size)
t.Cleanup(func() { runtime.FreeHandle(ptr) })
return uint32(ptr)
}
// generateTestPattern creates a test data pattern of specified size
func generateTestPattern(size int) []byte {
data := make([]byte, size)
for i := range data {
data[i] = byte(i % 256)
}
return data
}
// createPackedPtr creates a packed pointer with given address and size
func createPackedPtr(t *testing.T, runtime *qjs.Runtime, addr, size uint32) uint64 {
packedValue := uint64(addr)<<32 | uint64(size)
packedPtr := allocateMemoryPtr(t, runtime, 8)
err := runtime.Mem().WriteUint64(packedPtr, packedValue)
require.NoError(t, err)
return uint64(packedPtr)
}
// memTestUtil provides common memory testing utilities
type memTestUtil struct {
t *testing.T
runtime *qjs.Runtime
}
func newMemTestUtil(t *testing.T, runtime *qjs.Runtime) *memTestUtil {
return &memTestUtil{t: t, runtime: runtime}
}
func (m *memTestUtil) allocatePtr(size uint64) uint32 {
return allocateMemoryPtr(m.t, m.runtime, size)
}
func (m *memTestUtil) assertWriteRead(ptr uint32, data []byte) {
err := m.runtime.Mem().Write(ptr, data)
require.NoError(m.t, err)
readData, err := m.runtime.Mem().Read(ptr, uint64(len(data)))
require.NoError(m.t, err)
assert.Equal(m.t, data, readData)
}
func (m *memTestUtil) testErrorCondition(name string, testFunc func() error, expectedErr error) {
m.t.Run(name, func(t *testing.T) {
err := testFunc()
assert.ErrorIs(t, err, expectedErr, "Test %s should fail with expected error", name)
})
}
func (m *memTestUtil) testBoundaryCondition(typeName string, typeSize uint32, testFunc func(uint32) error) {
}
// Test data structures for table-driven tests
type memoryOperationTest struct {
name string
dataSize uint64
testData []byte
shouldError bool
errorType error
testFunc func(*memTestUtil, memoryOperationTest)
}
type primitiveTypeTest struct {
name string
size uint64
values []any
writeFunc func(*qjs.Runtime, uint32, any) error
readFunc func(*qjs.Runtime, uint32) (any, error)
specialTests func(*testing.T, *qjs.Runtime)
}
type stringTest struct {
name string
testString string
maxLen uint32
expectTruncated string
shouldError bool
errorType error
setupFunc func(*testing.T, *qjs.Runtime) uint32
}
type pointerTest struct {
name string
setupFunc func(*testing.T, *qjs.Runtime) uint64
expectAddr uint32
expectSize uint32
shouldPanic bool
testString string
}
// Main test functions with improved structure and names
func TestMem_ReadWrite(t *testing.T) {
runtime, _ := setupRuntime(t)
util := newMemTestUtil(t, runtime)
tests := []memoryOperationTest{
{
name: "basic_byte_operations",
dataSize: 100,
testData: []byte("Hello World"),
},
{
name: "empty_data_handling",
dataSize: 10,
testData: []byte{},
},
{
name: "large_data_operations",
dataSize: 1024,
testData: generateTestPattern(1024),
},
{
name: "null_pointer_read_error",
dataSize: 0,
testData: []byte{1, 2, 3},
shouldError: true,
errorType: qjs.ErrInvalidPointer,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
_, err := util.runtime.Mem().Read(0, uint64(len(tc.testData)))
assert.ErrorIs(t, err, tc.errorType)
},
},
{
name: "null_pointer_write_error",
dataSize: 0,
testData: []byte{1, 2, 3},
shouldError: true,
errorType: qjs.ErrInvalidPointer,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
err := util.runtime.Mem().Write(0, tc.testData)
assert.ErrorIs(t, err, tc.errorType)
},
},
{
name: "read_uint8_at_boundary",
dataSize: 0,
testData: []byte{},
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
mem := util.runtime.Mem()
memSize := mem.Size()
_, err := mem.ReadUint8(memSize)
assert.ErrorIs(t, err, tc.errorType)
},
},
{
name: "read_uint32_insufficient_bytes",
dataSize: 0,
testData: []byte{},
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
mem := util.runtime.Mem()
memSize := mem.Size()
if memSize >= 2 {
_, err := mem.ReadUint32(memSize - 2) // Only 2 bytes available, needs 4
assert.ErrorIs(t, err, tc.errorType)
}
},
},
{
name: "write_uint32_insufficient_space",
dataSize: 0,
testData: []byte{},
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
mem := util.runtime.Mem()
memSize := mem.Size()
if memSize >= 2 {
err := mem.WriteUint32(memSize-2, 42) // Only 2 bytes available, needs 4
assert.ErrorIs(t, err, tc.errorType)
}
},
},
{
name: "read_uint64_insufficient_bytes",
dataSize: 0,
testData: []byte{},
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
mem := util.runtime.Mem()
memSize := mem.Size()
if memSize >= 4 {
_, err := mem.ReadUint64(memSize - 4) // Only 4 bytes available, needs 8
assert.ErrorIs(t, err, tc.errorType)
}
},
},
{
name: "write_uint64_insufficient_space",
dataSize: 0,
testData: []byte{},
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
mem := util.runtime.Mem()
memSize := mem.Size()
if memSize >= 4 {
err := mem.WriteUint64(memSize-4, 42) // Only 4 bytes available, needs 8
assert.ErrorIs(t, err, tc.errorType)
}
},
},
{
name: "size_exceeds_max_uint32",
dataSize: 100,
testData: []byte{},
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
ptr := util.allocatePtr(tc.dataSize)
oversizedRead := uint64(math.MaxUint32) + 1
_, err := util.runtime.Mem().Read(ptr, oversizedRead)
assert.ErrorIs(t, err, tc.errorType)
},
},
{
name: "read_string_beyond_memory_bounds",
dataSize: 0,
testData: []byte{},
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
mem := util.runtime.Mem()
memSize := mem.Size()
// Test ReadString with a pointer beyond memory bounds
if memSize < math.MaxUint32-1000 {
beyondMemPtr := memSize + 100
_, err := mem.ReadString(beyondMemPtr, 10)
assert.ErrorIs(t, err, tc.errorType, "ReadString beyond memory should fail")
}
// Test ReadString with pointer definitely invalid
if memSize > 1 {
invalidPtr := memSize + 1
_, err := mem.ReadString(invalidPtr, 1)
assert.ErrorIs(t, err, tc.errorType, "ReadString with invalid pointer should fail")
}
},
},
{
name: "large_pointer_with_small_size",
dataSize: 0,
testData: []byte{},
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
testFunc: func(util *memTestUtil, tc memoryOperationTest) {
mem := util.runtime.Mem()
memSize := mem.Size()
// Use a large pointer value that's definitely out of bounds
var largePtr uint32
if memSize > 1000 {
largePtr = memSize + 1000
} else {
largePtr = memSize * 2 // Ensure it's beyond current memory
}
// Test Read with small size at large pointer
_, err := mem.Read(largePtr, 1)
assert.Error(t, err, "Read with large pointer should fail")
// Test WriteUint8 at large pointer
err = mem.WriteUint8(largePtr, 42)
assert.Error(t, err, "WriteUint8 with large pointer should fail")
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if tc.testFunc != nil {
tc.testFunc(util, tc)
return
}
ptr := util.allocatePtr(tc.dataSize)
util.assertWriteRead(ptr, tc.testData)
})
}
t.Run("must_operations", func(t *testing.T) {
ptr := util.allocatePtr(10)
testData := []byte{1, 2, 3, 4, 5}
util.runtime.Mem().MustWrite(ptr, testData)
readData := util.runtime.Mem().MustRead(ptr, uint64(len(testData)))
assert.Equal(t, testData, readData)
// Test panic conditions
assert.Panics(t, func() {
util.runtime.Mem().MustRead(0, 5)
})
assert.Panics(t, func() {
util.runtime.Mem().MustWrite(0, []byte{1, 2, 3})
})
})
}
func TestMem_Primitives(t *testing.T) {
runtime, _ := setupRuntime(t)
util := newMemTestUtil(t, runtime)
tests := []primitiveTypeTest{
{
name: "uint8",
size: 1,
values: []any{
uint8(0), uint8(127), uint8(255),
},
writeFunc: func(r *qjs.Runtime, ptr uint32, v any) error {
return r.Mem().WriteUint8(ptr, v.(uint8))
},
readFunc: func(r *qjs.Runtime, ptr uint32) (any, error) {
return r.Mem().ReadUint8(ptr)
},
},
{
name: "uint32",
size: 4,
values: []any{
uint32(0), uint32(0x12345678), uint32(math.MaxUint32),
},
writeFunc: func(r *qjs.Runtime, ptr uint32, v any) error {
return r.Mem().WriteUint32(ptr, v.(uint32))
},
readFunc: func(r *qjs.Runtime, ptr uint32) (any, error) {
return r.Mem().ReadUint32(ptr)
},
},
{
name: "uint64",
size: 8,
values: []any{
uint64(0), uint64(0x1234567890ABCDEF), uint64(math.MaxUint64),
},
writeFunc: func(r *qjs.Runtime, ptr uint32, v any) error {
return r.Mem().WriteUint64(ptr, v.(uint64))
},
readFunc: func(r *qjs.Runtime, ptr uint32) (any, error) {
return r.Mem().ReadUint64(ptr)
},
},
{
name: "float64",
size: 8,
values: []any{
0.0, 3.14159265358979, math.MaxFloat64,
math.SmallestNonzeroFloat64, -math.MaxFloat64,
math.Inf(1), math.Inf(-1),
},
writeFunc: func(r *qjs.Runtime, ptr uint32, v any) error {
return r.Mem().WriteFloat64(ptr, v.(float64))
},
readFunc: func(r *qjs.Runtime, ptr uint32) (any, error) {
return r.Mem().ReadFloat64(ptr)
},
specialTests: func(t *testing.T, runtime *qjs.Runtime) {
// NaN requires special handling since NaN != NaN
ptr := allocateMemoryPtr(t, runtime, 8)
err := runtime.Mem().WriteFloat64(ptr, math.NaN())
require.NoError(t, err)
readValue, err := runtime.Mem().ReadFloat64(ptr)
require.NoError(t, err)
assert.True(t, math.IsNaN(readValue), "NaN should be preserved")
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Test normal read/write operations
for _, value := range tc.values {
t.Run(fmt.Sprintf("%s_value_%v", tc.name, value), func(t *testing.T) {
ptr := util.allocatePtr(tc.size)
err := tc.writeFunc(runtime, ptr, value)
require.NoError(t, err, "Write operation should succeed")
readValue, err := tc.readFunc(runtime, ptr)
require.NoError(t, err, "Read operation should succeed")
assert.Equal(t, value, readValue, "Read value should match written value")
})
}
// Test error conditions using unified approach
util.testErrorCondition(
fmt.Sprintf("%s_null_pointer_read", tc.name),
func() error {
_, err := tc.readFunc(runtime, 0)
return err
},
qjs.ErrInvalidPointer,
)
util.testErrorCondition(
fmt.Sprintf("%s_null_pointer_write", tc.name),
func() error {
return tc.writeFunc(runtime, 0, tc.values[0])
},
qjs.ErrInvalidPointer,
)
// Test boundary conditions using utility
util.testBoundaryCondition(tc.name, uint32(tc.size), func(ptr uint32) error {
return tc.writeFunc(runtime, ptr, tc.values[0])
})
// Run special tests if provided
if tc.specialTests != nil {
t.Run(fmt.Sprintf("%s_special_cases", tc.name), func(t *testing.T) {
tc.specialTests(t, runtime)
})
}
})
}
}
func TestMem_Strings(t *testing.T) {
runtime, _ := setupRuntime(t)
util := newMemTestUtil(t, runtime)
tests := []stringTest{
{
name: "basic_string_operations",
testString: "Hello, World!",
},
{
name: "empty_string_handling",
testString: "",
},
{
name: "unicode_string_support",
testString: "Hello 世界 🌍",
},
{
name: "string_with_control_chars",
testString: "Line1\nLine2\rLine3\r\nCol1\tCol2",
},
{
name: "string_with_embedded_null",
testString: "Before\x00After",
expectTruncated: "Before", // Truncated at embedded null
},
{
name: "null_pointer_read_error",
testString: "test",
shouldError: true,
errorType: qjs.ErrInvalidPointer,
setupFunc: func(t *testing.T, runtime *qjs.Runtime) uint32 {
return 0
},
},
{
name: "null_pointer_write_error",
testString: "test",
shouldError: true,
errorType: qjs.ErrInvalidPointer,
},
{
name: "insufficient_space_error",
testString: "test",
shouldError: true,
errorType: qjs.ErrIndexOutOfRange,
setupFunc: func(t *testing.T, runtime *qjs.Runtime) uint32 {
return runtime.Mem().Size()
},
},
{
name: "zero_maxlen_handling",
testString: "test",
maxLen: 0,
},
{
name: "large_maxlen_handling",
testString: "test",
maxLen: 1000000,
},
{
name: "max_uint32_maxlen",
testString: "test",
maxLen: math.MaxUint32,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var ptr uint32
if tc.setupFunc != nil {
ptr = tc.setupFunc(t, runtime)
} else if !tc.shouldError || tc.name == "null_pointer_write_error" {
ptr = util.allocatePtr(uint64(len(tc.testString) + 1))
}
// Test error conditions
if tc.shouldError {
switch tc.name {
case "null_pointer_read_error":
_, err := runtime.Mem().ReadString(0, 10)
assert.ErrorIs(t, err, tc.errorType)
return
case "null_pointer_write_error":
err := runtime.Mem().WriteString(0, tc.testString)
assert.ErrorIs(t, err, tc.errorType)
return
case "insufficient_space_error":
err := runtime.Mem().WriteString(ptr, tc.testString)
assert.ErrorIs(t, err, tc.errorType)
return
}
}
// Normal string operations
err := runtime.Mem().WriteString(ptr, tc.testString)
require.NoError(t, err)
expectedStr := tc.testString
if tc.expectTruncated != "" {
expectedStr = tc.expectTruncated
}
maxLen := tc.maxLen
if maxLen == 0 && tc.name != "zero_maxlen_handling" {
maxLen = uint32(len(tc.testString) + 1)
}
if tc.name == "zero_maxlen_handling" {
str, err := runtime.Mem().ReadString(ptr, maxLen)
require.NoError(t, err)
assert.Empty(t, str)
return
}
readStr, err := runtime.Mem().ReadString(ptr, maxLen)
require.NoError(t, err)
assert.Equal(t, expectedStr, readStr)
// Verify null terminator for basic strings
if tc.name == "basic_string_operations" {
rawBytes, err := runtime.Mem().Read(ptr, uint64(len(tc.testString)+1))
require.NoError(t, err)
assert.Equal(t, byte(0), rawBytes[len(tc.testString)])
}
})
}
t.Run("missing_null_terminator_error", func(t *testing.T) {
ptr := util.allocatePtr(5)
runtime.Mem().MustWrite(ptr, []byte{65, 66, 67, 68, 69})
_, err := runtime.Mem().ReadString(ptr, 5)
assert.ErrorIs(t, err, qjs.ErrNoNullTerminator)
})
}
func TestMem_Pointers(t *testing.T) {
runtime, _ := setupRuntime(t)
tests := []pointerTest{
{
name: "valid_packed_data",
setupFunc: func(t *testing.T, runtime *qjs.Runtime) uint64 {
return createPackedPtr(t, runtime, 0x12345678, 0x87654321)
},
expectAddr: 0x12345678,
expectSize: 0x87654321,
},
{
name: "zero_packed_ptr",
setupFunc: func(t *testing.T, runtime *qjs.Runtime) uint64 {
return 0
},
expectAddr: 0,
expectSize: 0,
},
{
name: "little_endian_handling",
setupFunc: func(t *testing.T, runtime *qjs.Runtime) uint64 {
ptr := allocateMemoryPtr(t, runtime, 8)
err := runtime.Mem().Write(ptr, []byte{1, 2, 3, 4, 5, 6, 7, 8})
require.NoError(t, err)
return uint64(ptr)
},
expectAddr: 0x08070605,
expectSize: 0x04030201,
},
{
name: "out_of_bounds_panic",
setupFunc: func(t *testing.T, runtime *qjs.Runtime) uint64 {
return uint64(math.MaxUint32 - 3)
},
shouldPanic: true,
},
{
name: "valid_string_from_packed_ptr",
setupFunc: func(t *testing.T, runtime *qjs.Runtime) uint64 {
testStr := "Test string for packed ptr"
strPtr := allocateMemoryPtr(t, runtime, uint64(len(testStr)+1))
err := runtime.Mem().WriteString(strPtr, testStr)
require.NoError(t, err)
return createPackedPtr(t, runtime, strPtr, uint32(len(testStr)))
},
testString: "Test string for packed ptr",
},
{
name: "string_from_packed_ptr_zero_panic",
setupFunc: func(t *testing.T, runtime *qjs.Runtime) uint64 {
return 0
},
shouldPanic: true,
testString: "StringFromPackedPtr",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
packedPtr := tc.setupFunc(t, runtime)
if tc.shouldPanic {
if tc.testString != "" {
assert.Panics(t, func() {
runtime.Mem().StringFromPackedPtr(packedPtr)
})
} else {
assert.Panics(t, func() {
runtime.Mem().UnpackPtr(packedPtr)
})
}
return
}
if tc.testString != "" {
str := runtime.Mem().StringFromPackedPtr(packedPtr)
assert.Equal(t, tc.testString, str)
} else {
addr, size := runtime.Mem().UnpackPtr(packedPtr)
assert.Equal(t, tc.expectAddr, addr)
assert.Equal(t, tc.expectSize, size)
}
})
}
}
func TestMem_Boundaries(t *testing.T) {
runtime, _ := setupRuntime(t)
util := newMemTestUtil(t, runtime)
t.Run("memory_size_properties", func(t *testing.T) {
size := runtime.Mem().Size()
assert.Positive(t, size, "Memory size should be greater than 0")
runtime.Malloc(1024)
newSize := runtime.Mem().Size()
assert.GreaterOrEqual(t, newSize, size, "Memory size should not decrease after allocation")
})
// Consolidated boundary testing for all types
boundaryTests := []struct {
name string
typeSize uint32
testFunc func(ptr uint32) error
}{
{
name: "uint8",
typeSize: 1,
testFunc: func(ptr uint32) error {
return runtime.Mem().WriteUint8(ptr, 1)
},
},
{
name: "uint32",
typeSize: 4,
testFunc: func(ptr uint32) error {
return runtime.Mem().WriteUint32(ptr, 1)
},
},
{
name: "uint64",
typeSize: 8,
testFunc: func(ptr uint32) error {
return runtime.Mem().WriteUint64(ptr, 1)
},
},
{
name: "float64",
typeSize: 8,
testFunc: func(ptr uint32) error {
return runtime.Mem().WriteFloat64(ptr, 1.0)
},
},
}
for _, bt := range boundaryTests {
util.testBoundaryCondition(bt.name, bt.typeSize, bt.testFunc)
}
}
func TestMem_EdgeCases(t *testing.T) {
runtime, _ := setupRuntime(t)
util := newMemTestUtil(t, runtime)
edgeCases := []struct {
name string
testFunc func(*testing.T, *memTestUtil)
}{
{
name: "string_at_memory_boundary",
testFunc: func(t *testing.T, util *memTestUtil) {
ptr := util.allocatePtr(10)
testStr := "AB"
err := util.runtime.Mem().WriteString(ptr, testStr)
require.NoError(t, err)
readStr, err := util.runtime.Mem().ReadString(ptr, uint32(len(testStr)+1))
require.NoError(t, err)
assert.Equal(t, testStr, readStr)
// Test reading with large maxlen
readStr, err = util.runtime.Mem().ReadString(ptr, 1000000)
require.NoError(t, err)
assert.Equal(t, testStr, readStr)
},
},
{
name: "large_data_operations",
testFunc: func(t *testing.T, util *memTestUtil) {
largeSize := uint64(65536) // 64KB test
ptr := util.allocatePtr(largeSize)
testData := generateTestPattern(int(largeSize))
util.assertWriteRead(ptr, testData)
},
},
{
name: "multiple_allocations_integrity",
testFunc: func(t *testing.T, util *memTestUtil) {
const numAllocs = 100
const allocSize = uint64(64)
ptrs := make([]uint32, numAllocs)
expectedData := make([][]byte, numAllocs)
// Create multiple allocations with unique data
for i := range numAllocs {
ptrs[i] = util.allocatePtr(allocSize)
testData := make([]byte, allocSize)
for j := range testData {
testData[j] = byte(i + j)
}
expectedData[i] = testData
err := util.runtime.Mem().Write(ptrs[i], testData)
require.NoError(t, err)
}
// Verify data integrity across all allocations
for i := range numAllocs {
readData, err := util.runtime.Mem().Read(ptrs[i], allocSize)
require.NoError(t, err)
assert.Equal(t, expectedData[i], readData)
}
},
},
}
for _, tc := range edgeCases {
t.Run(tc.name, func(t *testing.T) {
tc.testFunc(t, util)
})
}
}