Debian Codegraph @

test/arenas/smoke.go

raw
1 // build -goexperiment arenas
2
3 // Copyright 2023 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 package main
8
9 import (
10 "arena"
11 "log"
12 "reflect"
13 )
14
15 func main() {
16 a := arena.NewArena()
17 defer a.Free()
18
19 const iValue = 10
20
21 i := arena.New[int](a)
22 *i = iValue
23
24 if *i != iValue {
25 // This test doesn't reasonably expect this to fail. It's more likely
26 // that *i crashes for some reason. Still, why not check it.
27 log.Fatalf("bad i value: got %d, want %d", *i, iValue)
28 }
29
30 const wantLen = 125
31 const wantCap = 1912
32
33 sl := arena.MakeSlice[*int](a, wantLen, wantCap)
34 if len(sl) != wantLen {
35 log.Fatalf("bad arena slice length: got %d, want %d", len(sl), wantLen)
36 }
37 if cap(sl) != wantCap {
38 log.Fatalf("bad arena slice capacity: got %d, want %d", cap(sl), wantCap)
39 }
40 sl = sl[:cap(sl)]
41 for j := range sl {
42 sl[j] = i
43 }
44 for j := range sl {
45 if *sl[j] != iValue {
46 // This test doesn't reasonably expect this to fail. It's more likely
47 // that sl[j] crashes for some reason. Still, why not check it.
48 log.Fatalf("bad sl[j] value: got %d, want %d", *sl[j], iValue)
49 }
50 }
51
52 t := reflect.TypeOf(int(0))
53 v := reflect.ArenaNew(a, t)
54 if want := reflect.PointerTo(t); v.Type() != want {
55 log.Fatalf("unexpected type for arena-allocated value: got %s, want %s", v.Type(), want)
56 }
57 i2 := v.Interface().(*int)
58 *i2 = iValue
59
60 if *i2 != iValue {
61 // This test doesn't reasonably expect this to fail. It's more likely
62 // that *i crashes for some reason. Still, why not check it.
63 log.Fatalf("bad i2 value: got %d, want %d", *i2, iValue)
64 }
65 }