File: arena_test.go

package info (click to toggle)
golang-github-huandu-go-clone 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,410 bytes parent folder | download
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
// Copyright 2023 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.

//go:build go1.20 && goexperiment.arenas
// +build go1.20,goexperiment.arenas

package clone

import (
	"arena"
	"reflect"
	"runtime"
	"testing"
	"unsafe"

	"github.com/huandu/go-assert"
)

func TestArenaClone(t *testing.T) {
	a := assert.New(t)

	type FooInner struct {
		Value float64
	}

	type Foo struct {
		A string
		B []int
		C *FooInner
		D map[int]string
	}

	foo := &Foo{
		A: "hello",
		B: []int{1, 2, 3},
		C: &FooInner{
			Value: 45.6,
		},
		D: map[int]string{
			7: "7",
		},
	}

	ar := arena.NewArena()

	cloned := ArenaClone(ar, foo)
	a.Equal(foo, cloned)

	// If a pointer is not allocated by arena, arena.Clone() will return the pointer as it is.
	// Use this feature to check whether a pointer is allocated by arena.
	prevStr := foo.A
	str := arena.Clone(cloned.A)
	a.Assert(((*reflect.StringHeader)(unsafe.Pointer(&str))).Data != ((*reflect.StringHeader)(unsafe.Pointer(&prevStr))).Data)

	a.Assert(arena.Clone(cloned) != foo)

	slice := arena.Clone(cloned.B)
	a.Assert(&slice[0] != &foo.B[0])

	a.Assert(arena.Clone(cloned.C) != foo.C)

	prevStr = foo.D[7]
	str = arena.Clone(cloned.D[7])
	a.Assert(((*reflect.StringHeader)(unsafe.Pointer(&str))).Data != ((*reflect.StringHeader)(unsafe.Pointer(&prevStr))).Data)

	// Make sure ar is alive.
	runtime.KeepAlive(ar)
}