-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflextree_test.go
More file actions
709 lines (594 loc) · 17 KB
/
flextree_test.go
File metadata and controls
709 lines (594 loc) · 17 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
package yogadb
import (
"fmt"
"time"
"unsafe"
//"github.com/glycerine/vfs"
"testing"
)
var _ = time.Now
const testFlexTreeMaxExtentSizeLimit = (64 << 20)
// Helpers for generating pseudo random test data
// in a reproducible fashion.
var globalTestPRNG *prng
var globalR uint64
func resetRand() {
var seed [32]byte
seed[0] = 42
globalTestPRNG = newPRNG(seed)
}
func init() {
resetRand()
}
func randInt() uint64 {
return globalTestPRNG.Uint64()
}
func queryResultEqual(rr1, rr2 *FlextreeQueryResult) bool {
if rr1 == nil && rr2 == nil {
return true
}
if rr1 != rr2 && (rr1 == nil || rr2 == nil) {
return false
}
if rr1.Count != rr2.Count {
return false
}
for i := uint64(0); i < rr1.Count; i++ {
if rr1.V[i].Poff != rr2.V[i].Poff || rr1.V[i].Len != rr2.V[i].Len {
return false
}
}
return true
}
func randomInsert(t *testing.T, ft *FlexTree, bf *bruteForce, count uint64) {
if ft != nil {
resetRand()
//t0 := time.Now()
ft.Insert(0, 0, 4)
var maxLoff uint64 = 4
for i := uint64(0); i < count; i++ {
len := uint32(randInt()%1000 + 1)
ft.InsertWTag(maxLoff%1000, maxLoff, len, uint16(i%0xffff))
maxLoff += uint64(len)
}
//vv("insert to flextree %v elapsed\n", time.Since(t0))
}
if bf != nil {
resetRand()
bf.Insert(0, 0, 4)
var maxLoff uint64 = 4
for i := uint64(0); i < count; i++ {
len := uint32(randInt()%1000 + 1)
bf.InsertWTag(maxLoff%1000, maxLoff, len, uint16(i%0xffff))
maxLoff += uint64(len)
}
}
}
func sequentialQuery(t *testing.T, ft *FlexTree, bf *bruteForce, totalSize uint64) {
if ft == nil || bf == nil {
return
}
for i := uint64(0); i < totalSize; i++ {
globalR += ft.PQuery(i)
}
for i := uint64(0); i < totalSize; i++ {
globalR += bf.PQuery(i)
}
for i := int64(totalSize); i >= 0; i-- {
fr := ft.PQuery(uint64(i))
br := bf.PQuery(uint64(i))
if fr != br {
t.Fatalf("Error encountered on %d %d %d", i, fr, br)
}
if i == 0 {
break
}
}
}
func randomDelete(t *testing.T, ft *FlexTree, bf *bruteForce, totalSize uint64, count uint64) {
if ft == nil || bf == nil {
return
}
osize := totalSize
if ft != nil {
resetRand()
for i := uint64(0); i < count; i++ {
tmp := randInt() % totalSize
tmp2 := randInt()%10 + 1
ft.Delete(tmp, tmp2)
totalSize -= tmp2
}
}
totalSize = osize
if bf != nil {
resetRand()
for i := uint64(0); i < count; i++ {
tmp := randInt() % totalSize
tmp2 := randInt()%10 + 1
bf.Delete(tmp, tmp2)
totalSize -= tmp2
}
}
}
// checkFlexTreeContiguity verifies FlexTree extent integrity independent of any oracle.
// Walks the leaf linked list and checks:
// - INV-FT-1: Within each leaf, extents are sorted and contiguous (no gaps, no overlaps)
// - INV-FT-2: No zero-length extents
// - INV-FT-3: Extent count matches node.Count
// - INV-FT-4: PQuery succeeds for every loff in [0, MaxLoff) (full coverage, no holes)
// - INV-FT-5: MaxLoff == sum of all extent lengths across all leaves
func checkFlexTreeContiguity(t testing.TB, ft *FlexTree, opDesc string) {
t.Helper()
if ft.MaxLoff == 0 {
return
}
totalExtentLen := uint64(0)
leafCount := 0
nodeID := ft.LeafHead
for nodeID != IllegalID {
leaf := ft.GetLeaf(nodeID)
leafCount++
// INV-FT-3: Count is sane
if leaf.Count == 0 && leafCount > 1 {
// Only the very first leaf in an empty tree can have count 0
t.Fatalf("[INV-FT-3] %s: leaf %v has Count=0", opDesc, nodeID)
}
le := leaf
for i := uint32(0); i < leaf.Count; i++ {
ext := &le.Extents[i]
// INV-FT-2: No zero-length extents
if ext.Len == 0 {
t.Fatalf("[INV-FT-2] %s: leaf %v extent[%d] has Len=0 (Loff=%d)",
opDesc, nodeID, i, ext.Loff)
}
totalExtentLen += uint64(ext.Len)
// INV-FT-1: Intra-leaf contiguity
if i+1 < leaf.Count {
next := &le.Extents[i+1]
end := ext.Loff + ext.Len
if end != next.Loff {
t.Fatalf("[INV-FT-1] %s: leaf %v extent[%d] ends at %d but extent[%d] starts at %d (gap/overlap of %d)",
opDesc, nodeID, i, end, i+1, next.Loff, int64(next.Loff)-int64(end))
}
}
}
nodeID = leaf.Next
}
// INV-FT-5: Total extent length == MaxLoff
if totalExtentLen != ft.MaxLoff {
t.Fatalf("[INV-FT-5] %s: sum of extent lengths = %d but MaxLoff = %d",
opDesc, totalExtentLen, ft.MaxLoff)
}
// INV-FT-4: Full PQuery coverage - every loff maps to a valid poff.
// (Spot-check: test 1024 evenly-spaced positions plus boundaries)
step := ft.MaxLoff / 1024
if step == 0 {
step = 1
}
for loff := uint64(0); loff < ft.MaxLoff; loff += step {
poff := ft.PQuery(loff)
if poff == ^uint64(0) {
t.Fatalf("[INV-FT-4] %s: PQuery(%d) returned not-found (MaxLoff=%d)",
opDesc, loff, ft.MaxLoff)
}
}
// Check last valid position
if ft.MaxLoff > 0 {
poff := ft.PQuery(ft.MaxLoff - 1)
if poff == ^uint64(0) {
t.Fatalf("[INV-FT-4] %s: PQuery(%d) returned not-found (MaxLoff=%d)",
opDesc, ft.MaxLoff-1, ft.MaxLoff)
}
}
}
func TestFlextree_Test0(t *testing.T) {
count := uint64(50000) // Lower count for faster general tests initially
vv("---test0 insertion and point lookup %v ---", count)
fs, dir := newTestFS(t)
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
randomInsert(t, ft, nil, count)
checkFlexTreeContiguity(t, ft, "Test0-afterInsert")
randomDelete(t, ft, nil, ft.GetMaxLoff(), count)
checkFlexTreeContiguity(t, ft, "Test0-afterDelete")
sequentialQuery(t, ft, nil, ft.GetMaxLoff())
loff := uint64(0)
for ft.GetMaxLoff() > 100 {
loff = (loff + 0xabcd12) % (ft.GetMaxLoff() - 100)
ft.Delete(loff, 100)
}
ft.Delete(0, ft.GetMaxLoff()-10)
ft.Print()
ft.Delete(0, ft.GetMaxLoff())
ft.Print()
ft.Close()
}
func TestFlextree_Test1(t *testing.T) {
count := uint64(500)
fs, dir := newTestFS(t)
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
bf := openBruteForce(DefaultFlexTreeMaxExtentSizeLimit)
randomInsert(t, ft, bf, count)
if ft.GetMaxLoff() != bf.GetMaxLoff() {
t.Fatalf("MaxLoff mismatch: %d != %d", ft.GetMaxLoff(), bf.GetMaxLoff())
}
// ft.GetMaxLoff() is currently 0 in the stub, causing sequentialQuery to do nothing. Let's force it to fail if it's 0 after inserts.
if ft.GetMaxLoff() == 0 {
t.Fatalf("MaxLoff should not be zero after inserts")
}
sequentialQuery(t, ft, bf, ft.GetMaxLoff())
bf.Close()
ft.Close()
}
func randomPDelete(t *testing.T, ft *FlexTree, bf *bruteForce, totalSize uint64, count uint64) {
if ft != nil {
resetRand()
for i := uint64(0); i < count; i++ {
tmp := randInt() % totalSize
ft.PDelete(tmp)
}
}
if bf != nil {
resetRand()
for i := uint64(0); i < count; i++ {
tmp := randInt() % totalSize
bf.PDelete(tmp)
}
}
}
func TestFlextree_Test2(t *testing.T) {
count := uint64(500)
fs, dir := newTestFS(t)
_ = dir
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
bf := openBruteForce(DefaultFlexTreeMaxExtentSizeLimit)
randomInsert(t, ft, bf, count)
checkFlexTreeContiguity(t, ft, "Test2-afterInsert")
randomPDelete(t, ft, bf, ft.GetMaxLoff(), count)
checkFlexTreeContiguity(t, ft, "Test2-afterPDelete")
if ft.GetMaxLoff() != bf.GetMaxLoff() {
t.Fatalf("MaxLoff mismatch: %d != %d", ft.GetMaxLoff(), bf.GetMaxLoff())
}
sequentialQuery(t, ft, bf, ft.GetMaxLoff())
bf.Close()
ft.Close()
}
func TestFlextree_Test3(t *testing.T) {
count := uint64(500)
fs, dir := newTestFS(t)
_ = dir
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
bf := openBruteForce(DefaultFlexTreeMaxExtentSizeLimit)
randomInsert(t, ft, bf, count)
checkFlexTreeContiguity(t, ft, "Test3-afterInsert")
randomDelete(t, ft, bf, ft.GetMaxLoff(), count)
checkFlexTreeContiguity(t, ft, "Test3-afterDelete")
if ft.GetMaxLoff() != bf.GetMaxLoff() {
t.Fatalf("MaxLoff mismatch: %d != %d", ft.GetMaxLoff(), bf.GetMaxLoff())
}
sequentialQuery(t, ft, bf, ft.GetMaxLoff())
bf.Close()
ft.Close()
}
func randomRangeQuery(t *testing.T, ft *FlexTree, bf *bruteForce, totalSize uint64, count uint64) {
if ft == nil || bf == nil {
return
}
resetRand()
for i := uint64(0); i < count; i++ {
loff := randInt() % totalSize
len := randInt() % 100
fr := ft.Query(loff, len)
br := bf.Query(loff, len)
if !queryResultEqual(fr, br) {
t.Errorf("Range query mismatch for loff %d len %d", loff, len)
if fr != nil {
t.Errorf("FR: %+v, V: %+v", fr, fr.V)
} else {
t.Errorf("FR is nil")
}
if br != nil {
t.Errorf("BR: %+v, V: %+v", br, br.V)
} else {
t.Errorf("BR is nil")
}
t.FailNow()
}
}
}
func TestFlextree_Test4(t *testing.T) {
count := uint64(500)
fs, dir := newTestFS(t)
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
bf := openBruteForce(DefaultFlexTreeMaxExtentSizeLimit)
randomInsert(t, ft, bf, count)
randomDelete(t, ft, bf, ft.GetMaxLoff(), count)
if ft.GetMaxLoff() != bf.GetMaxLoff() {
t.Fatalf("MaxLoff mismatch: %d != %d", ft.GetMaxLoff(), bf.GetMaxLoff())
}
randomRangeQuery(t, ft, bf, ft.GetMaxLoff(), count)
bf.Close()
ft.Close()
}
func TestFlextree_Test6(t *testing.T) {
count := uint64(500)
fs, dir := newTestFS(t)
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
bf := openBruteForce(DefaultFlexTreeMaxExtentSizeLimit)
randomInsert(t, ft, bf, count)
r1 := ft.Insert(1<<34, 1<<40, 50)
if r1 != 0 {
t.Fatalf("ft insert hole failed")
}
r2 := bf.Insert(1<<34, 1<<40, 50)
if r2 != 0 {
t.Fatalf("bf insert hole failed")
}
randomRangeQuery(t, ft, bf, ft.GetMaxLoff(), count)
bf.Close()
ft.Close()
}
func TestFlextree_Test7(t *testing.T) {
count := uint64(500)
fs, dir := newTestFS(t)
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
bf := openBruteForce(DefaultFlexTreeMaxExtentSizeLimit)
randomInsert(t, ft, bf, count)
ft.Delete(ft.GetMaxLoff()/4, ft.GetMaxLoff()/4*3)
bf.Delete(bf.GetMaxLoff()/4, bf.GetMaxLoff()/4*3)
if ft.GetMaxLoff() != bf.GetMaxLoff() {
t.Fatalf("MaxLoff mismatch: %d != %d", ft.GetMaxLoff(), bf.GetMaxLoff())
}
sequentialQuery(t, ft, bf, ft.GetMaxLoff())
bf.Close()
ft.Close()
}
func sequentialTagQuery(t *testing.T, ft *FlexTree, bf *bruteForce, totalSize uint64) {
if ft == nil || bf == nil {
return
}
ft.SetTag(ft.GetMaxLoff()-1, 0xffff)
bf.SetTag(bf.GetMaxLoff()-1, 0xffff)
for i := uint64(0); i < totalSize; i++ {
ftTag, fr := ft.GetTag(i)
bfTag, br := bf.GetTag(i)
if fr != br || ftTag != bfTag {
t.Fatalf("Tag mismatch on loff %d: fr %d br %d ftTag %d bfTag %d", i, fr, br, ftTag, bfTag)
}
}
}
func TestFlextree_Test8(t *testing.T) {
count := uint64(500)
fs, dir := newTestFS(t)
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
bf := openBruteForce(DefaultFlexTreeMaxExtentSizeLimit)
randomInsert(t, ft, bf, count)
randomDelete(t, ft, bf, ft.GetMaxLoff(), count)
if ft.GetMaxLoff() != bf.GetMaxLoff() {
t.Fatalf("MaxLoff mismatch: %d != %d", ft.GetMaxLoff(), bf.GetMaxLoff())
}
sequentialTagQuery(t, ft, bf, ft.GetMaxLoff())
bf.Close()
ft.Close()
}
// randomAppend inserts sequential extents at the end of the tree
// (append pattern, not random-offset insert). Mirrors C's random_append().
func randomAppend(t *testing.T, ft *FlexTree, bf *bruteForce, count uint64) {
if ft != nil {
resetRand()
ft.Insert(0, 0, 4)
var maxLoff uint64 = 4
for i := uint64(0); i < count; i++ {
len := uint32(randInt()%1000 + 1)
ft.InsertWTag(maxLoff, maxLoff, len, uint16(i%0xffff))
maxLoff += uint64(len)
}
}
if bf != nil {
resetRand()
bf.Insert(0, 0, 4)
var maxLoff uint64 = 4
for i := uint64(0); i < count; i++ {
len := uint32(randInt()%1000 + 1)
bf.InsertWTag(maxLoff, maxLoff, len, uint16(i%0xffff))
maxLoff += uint64(len)
}
}
}
// countLeafNodes recursively counts leaf nodes reachable from root.
// Mirrors C's count_leaf_nodes().
func countLeafNodes(tree *FlexTree, id NodeID) uint64 {
if id.IsLeaf() {
return 1
}
ie := tree.GetInternal(id)
var c uint64
for i := uint32(0); i < ie.Count+1; i++ {
c += countLeafNodes(tree, ie.Children[i].NodeID)
}
return c
}
// countLeafNodesLL counts leaf nodes by walking the linked list from LeafHead.
func countLeafNodesLL(tree *FlexTree) uint64 {
var c uint64
id := tree.LeafHead
for !id.IsIllegal() {
le := tree.GetLeaf(id)
c++
id = le.Next
}
return c
}
// flextreeCheck validates tree integrity by summing extent lengths via Pos API.
// Mirrors C's flextree_check().
func flextreeCheck(t *testing.T, tree *FlexTree) {
t.Helper()
var totalLen uint64
pos := tree.PosGet(0)
for pos.Valid() {
ext := &pos.node.Extents[pos.Idx]
totalLen += uint64(ext.Len)
pos.ForwardExtent()
}
if tree.GetMaxLoff() != totalLen {
t.Fatalf("flextreeCheck: max_loff=%d but total extent len=%d", tree.GetMaxLoff(), totalLen)
}
}
// TestFlextree_Test5_CoW is the same as Test5 but uses CoW page-based
// persistence instead of greenpack serialization.
func TestFlextree_Test5_CoW(t *testing.T) {
count := uint64(5000)
fs, dir := newTestFS(t)
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
bf := openBruteForce(DefaultFlexTreeMaxExtentSizeLimit)
randomInsert(t, ft, bf, count)
// Count leaves via linked list and via tree recursion.
c1 := countLeafNodesLL(ft)
c2 := countLeafNodes(ft, ft.Root)
if c1 != c2 {
t.Fatalf("linked list count %d != tree walk count %d", c1, c2)
}
// Sync and reopen via CoW.
panicOn(ft.SyncCoW())
panicOn(ft.CloseCoW())
ft, err = OpenFlexTreeCoW(dir, fs)
panicOn(err)
// Verify linked list count survives CoW persistence.
c1r := countLeafNodesLL(ft)
if c1r != c2 {
t.Fatalf("after CoW reload: linked list count %d != original tree walk count %d", c1r, c2)
}
// Delete 3/4 of data.
ft.Delete(ft.GetMaxLoff()/4, ft.GetMaxLoff()/4*3)
bf.Delete(bf.GetMaxLoff()/4, bf.GetMaxLoff()/4*3)
c2 = countLeafNodes(ft, ft.Root)
// Sync and reopen via CoW.
panicOn(ft.SyncCoW())
panicOn(ft.CloseCoW())
ft, err = OpenFlexTreeCoW(dir, fs)
panicOn(err)
// Verify MaxLoff matches oracle.
if ft.GetMaxLoff() != bf.GetMaxLoff() {
t.Fatalf("MaxLoff mismatch: ft=%d bf=%d", ft.GetMaxLoff(), bf.GetMaxLoff())
}
sequentialQuery(t, ft, bf, ft.GetMaxLoff())
c1 = countLeafNodesLL(ft)
if c1 != c2 {
t.Fatalf("after delete+CoW reload: linked list count %d != tree walk count %d", c1, c2)
}
// Delete everything.
ft.Delete(0, ft.GetMaxLoff())
bf.Delete(0, bf.GetMaxLoff())
c2 = countLeafNodes(ft, ft.Root)
// Sync and reopen via CoW.
panicOn(ft.SyncCoW())
panicOn(ft.CloseCoW())
ft, err = OpenFlexTreeCoW(dir, fs)
panicOn(err)
if ft.GetMaxLoff() != bf.GetMaxLoff() {
t.Fatalf("final MaxLoff mismatch: ft=%d bf=%d", ft.GetMaxLoff(), bf.GetMaxLoff())
}
c1 = countLeafNodesLL(ft)
if c1 != c2 {
t.Fatalf("final: linked list count %d != tree walk count %d", c1, c2)
}
sequentialQuery(t, ft, bf, ft.GetMaxLoff())
bf.Close()
panicOn(ft.CloseCoW())
}
// TestFlextree_Test9 tests large persistent tree with repeated append/insert
// cycles and integrity checks via Pos API.
// Mirrors C test9 at reduced scale (C does 2.6B inserts + 10K rounds).
func TestFlextree_Test9(t *testing.T) {
count := uint64(500)
fs, dir := newTestFS(t)
// Phase 1: sequential inserts with 128KB max extent size.
ft, err := OpenFlexTreeCoW(dir, fs)
panicOn(err)
ft.MaxExtentSize = 128 << 10
fillCount := uint64(50000)
for i := uint64(0); i < fillCount; i++ {
ft.Insert(i*156, i*156, 156)
}
//panicOn(saveFlexTree(ft, fn))
ft.SyncCoW()
// Phase 2: repeated rounds of append, close/reopen, insert, close/reopen, check.
ft, err = OpenFlexTreeCoW(dir, fs)
panicOn(err)
rounds := 10
for r := 0; r < rounds; r++ {
randomAppend(t, ft, nil, count)
//panicOn(saveFlexTree(ft, fn))
ft.SyncCoW()
ft, err = OpenFlexTreeCoW(dir, fs)
panicOn(err)
randomInsert(t, ft, nil, count)
ft.SyncCoW()
//panicOn(saveFlexTree(ft, fn))
ft, err = OpenFlexTreeCoW(dir, fs)
panicOn(err)
flextreeCheck(t, ft)
checkFlexTreeContiguity(t, ft, fmt.Sprintf("Test9-round%d", r))
}
ft.Close()
}
func TestEndianMatchesC(t *testing.T) {
ext := FlexTreeExtent{
Loff: 0x11223344,
Len: 0x55667788,
}
ext.SetTag(0x99AA)
ext.SetAddress(0xBEEFCAFE)
ext.SetHole(true)
// Cast the struct's memory pointer to a raw byte slice of size 16
extSize := unsafe.Sizeof(ext)
extBytes := unsafe.Slice((*byte)(unsafe.Pointer(&ext)), extSize)
expect := `44 33 22 11 88 77 66 55 AA 99 FE CA EF BE 00 80 `
var obs string
for _, b := range extBytes {
obs += fmt.Sprintf("%02X ", b)
}
//fmt.Printf("Go Output: ")
//fmt.Println(obs)
if obs != expect {
panicf("mismatch! obs(%v) != expected(%v)", obs, expect)
}
}
/* the C to check that we emulate the tag/poff bit packing correctly.
#include <stdio.h>
#include <stdint.h>
struct flextree_extent {
uint32_t loff;
uint32_t len;
uint64_t tag :16;
uint64_t poff :48; // 47 bits for address, 1 bit for hole
} __attribute__((packed));
int main() {
struct flextree_extent ext = {0};
// Assign recognizable test values
ext.loff = 0x11223344;
ext.len = 0x55667788;
ext.tag = 0x99AA;
// Address: 0xBEEFCAFE (Fits well within 47 bits)
uint64_t address = 0xBEEFCAFE;
// Shift a 1 into the 48th position (bit 47) for the hole flag
uint64_t hole_bit = 1ULL << 47;
ext.poff = hole_bit | address;
// Treat the struct as an array of 16 bytes and print them
uint8_t *bytes = (uint8_t *)&ext;
printf("C Output: ");
for (int i = 0; i < 16; i++) {
printf("%02X ", bytes[i]);
}
printf("\n");
return 0;
}
*/