Debian Codegraph @

gopls/internal/util/frob/frob_test.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package frob_test
6
7 import (
8 "math"
9 "reflect"
10 "testing"
11
12 "golang.org/x/tools/gopls/internal/util/frob"
13 )
14
15 func TestBasics(t *testing.T) {
16 type Basics struct {
17 A []*string
18 B [2]int
19 C *Basics
20 D map[string]int
21 E []byte
22 F []string
23 }
24 codec := frob.CodecFor[Basics]()
25
26 s1, s2 := "hello", "world"
27 x := Basics{
28 A: []*string{&s1, nil, &s2},
29 B: [...]int{1, 2},
30 C: &Basics{
31 B: [...]int{3, 4},
32 D: map[string]int{"one": 1},
33 },
34 E: []byte("hello"),
35 F: []string{s1, s2},
36 }
37 y := codec.Decode(codec.Encode(x))
38 if !reflect.DeepEqual(x, y) {
39 t.Fatalf("bad roundtrip: got %#v, want %#v", y, x)
40 }
41 }
42
43 func TestInts(t *testing.T) {
44 type Ints struct {
45 U uint
46 U8 uint8
47 U16 uint16
48 U32 uint32
49 U64 uint64
50 UP uintptr
51 I int
52 I8 int8
53 I16 int16
54 I32 int32
55 I64 int64
56 F32 float32
57 F64 float64
58 C64 complex64
59 C128 complex128
60 }
61 codec := frob.CodecFor[Ints]()
62
63 // maxima
64 max1 := Ints{
65 U: math.MaxUint,
66 U8: math.MaxUint8,
67 U16: math.MaxUint16,
68 U32: math.MaxUint32,
69 U64: math.MaxUint64,
70 UP: math.MaxUint,
71 I: math.MaxInt,
72 I8: math.MaxInt8,
73 I16: math.MaxInt16,
74 I32: math.MaxInt32,
75 I64: math.MaxInt64,
76 F32: math.MaxFloat32,
77 F64: math.MaxFloat64,
78 C64: complex(math.MaxFloat32, math.MaxFloat32),
79 C128: complex(math.MaxFloat64, math.MaxFloat64),
80 }
81 max2 := codec.Decode(codec.Encode(max1))
82 if !reflect.DeepEqual(max1, max2) {
83 t.Fatalf("max: bad roundtrip: got %#v, want %#v", max2, max1)
84 }
85
86 // minima
87 min1 := Ints{
88 I: math.MinInt,
89 I8: math.MinInt8,
90 I16: math.MinInt16,
91 I32: math.MinInt32,
92 I64: math.MinInt64,
93 F32: -math.MaxFloat32,
94 F64: -math.MaxFloat32,
95 C64: complex(-math.MaxFloat32, -math.MaxFloat32),
96 C128: complex(-math.MaxFloat64, -math.MaxFloat64),
97 }
98 min2 := codec.Decode(codec.Encode(min1))
99 if !reflect.DeepEqual(min1, min2) {
100 t.Fatalf("min: bad roundtrip: got %#v, want %#v", min2, min1)
101 }
102
103 // negatives (other than MinInt), to exercise conversions
104 neg1 := Ints{
105 I: -1,
106 I8: -1,
107 I16: -1,
108 I32: -1,
109 I64: -1,
110 }
111 neg2 := codec.Decode(codec.Encode(neg1))
112 if !reflect.DeepEqual(neg1, neg2) {
113 t.Fatalf("neg: bad roundtrip: got %#v, want %#v", neg2, neg1)
114 }
115 }