golang.org/x/tools/gopls@v0.15.3/internal/util/frob/frob_test.go (about) 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 var y Basics 38 codec.Decode(codec.Encode(x), &y) 39 if !reflect.DeepEqual(x, y) { 40 t.Fatalf("bad roundtrip: got %#v, want %#v", y, x) 41 } 42 } 43 44 func TestInts(t *testing.T) { 45 type Ints struct { 46 U uint 47 U8 uint8 48 U16 uint16 49 U32 uint32 50 U64 uint64 51 UP uintptr 52 I int 53 I8 int8 54 I16 int16 55 I32 int32 56 I64 int64 57 F32 float32 58 F64 float64 59 C64 complex64 60 C128 complex128 61 } 62 codec := frob.CodecFor[Ints]() 63 64 // maxima 65 max1 := Ints{ 66 U: math.MaxUint, 67 U8: math.MaxUint8, 68 U16: math.MaxUint16, 69 U32: math.MaxUint32, 70 U64: math.MaxUint64, 71 UP: math.MaxUint, 72 I: math.MaxInt, 73 I8: math.MaxInt8, 74 I16: math.MaxInt16, 75 I32: math.MaxInt32, 76 I64: math.MaxInt64, 77 F32: math.MaxFloat32, 78 F64: math.MaxFloat64, 79 C64: complex(math.MaxFloat32, math.MaxFloat32), 80 C128: complex(math.MaxFloat64, math.MaxFloat64), 81 } 82 var max2 Ints 83 codec.Decode(codec.Encode(max1), &max2) 84 if !reflect.DeepEqual(max1, max2) { 85 t.Fatalf("max: bad roundtrip: got %#v, want %#v", max2, max1) 86 } 87 88 // minima 89 min1 := Ints{ 90 I: math.MinInt, 91 I8: math.MinInt8, 92 I16: math.MinInt16, 93 I32: math.MinInt32, 94 I64: math.MinInt64, 95 F32: -math.MaxFloat32, 96 F64: -math.MaxFloat32, 97 C64: complex(-math.MaxFloat32, -math.MaxFloat32), 98 C128: complex(-math.MaxFloat64, -math.MaxFloat64), 99 } 100 var min2 Ints 101 codec.Decode(codec.Encode(min1), &min2) 102 if !reflect.DeepEqual(min1, min2) { 103 t.Fatalf("min: bad roundtrip: got %#v, want %#v", min2, min1) 104 } 105 106 // negatives (other than MinInt), to exercise conversions 107 neg1 := Ints{ 108 I: -1, 109 I8: -1, 110 I16: -1, 111 I32: -1, 112 I64: -1, 113 } 114 var neg2 Ints 115 codec.Decode(codec.Encode(neg1), &neg2) 116 if !reflect.DeepEqual(neg1, neg2) { 117 t.Fatalf("neg: bad roundtrip: got %#v, want %#v", neg2, neg1) 118 } 119 }