| 1 |
// Copyright 2024 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 pkgbits_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/pkgbits" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestRoundTrip(t *testing.T) { |
| 14 |
for _, version := range []pkgbits.Version{ |
| 15 |
pkgbits.V0, |
| 16 |
pkgbits.V1, |
| 17 |
pkgbits.V2, |
| 18 |
} { |
| 19 |
pw := pkgbits.NewPkgEncoder(version, -1) |
| 20 |
w := pw.NewEncoder(pkgbits.SectionMeta, pkgbits.SyncPublic) |
| 21 |
w.Flush() |
| 22 |
|
| 23 |
var b strings.Builder |
| 24 |
_ = pw.DumpTo(&b) |
| 25 |
input := b.String() |
| 26 |
|
| 27 |
pr := pkgbits.NewPkgDecoder("package_id", input) |
| 28 |
r := pr.NewDecoder(pkgbits.SectionMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic) |
| 29 |
|
| 30 |
if r.Version() != w.Version() { |
| 31 |
t.Errorf("Expected reader version %d to be the writer version %d", r.Version(), w.Version()) |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// Type checker to enforce that know V* have the constant values they must have. |
| 37 |
var _ [0]bool = [pkgbits.V0]bool{} |
| 38 |
var _ [1]bool = [pkgbits.V1]bool{} |
| 39 |
|
| 40 |
func TestVersions(t *testing.T) { |
| 41 |
type vfpair struct { |
| 42 |
v pkgbits.Version |
| 43 |
f pkgbits.Field |
| 44 |
} |
| 45 |
|
| 46 |
// has field tests |
| 47 |
for _, c := range []vfpair{ |
| 48 |
{pkgbits.V1, pkgbits.Flags}, |
| 49 |
{pkgbits.V2, pkgbits.Flags}, |
| 50 |
{pkgbits.V0, pkgbits.HasInit}, |
| 51 |
{pkgbits.V1, pkgbits.HasInit}, |
| 52 |
{pkgbits.V0, pkgbits.DerivedFuncInstance}, |
| 53 |
{pkgbits.V1, pkgbits.DerivedFuncInstance}, |
| 54 |
{pkgbits.V0, pkgbits.DerivedInfoNeeded}, |
| 55 |
{pkgbits.V1, pkgbits.DerivedInfoNeeded}, |
| 56 |
{pkgbits.V2, pkgbits.AliasTypeParamNames}, |
| 57 |
} { |
| 58 |
if !c.v.Has(c.f) { |
| 59 |
t.Errorf("Expected version %v to have field %v", c.v, c.f) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// does not have field tests |
| 64 |
for _, c := range []vfpair{ |
| 65 |
{pkgbits.V0, pkgbits.Flags}, |
| 66 |
{pkgbits.V2, pkgbits.HasInit}, |
| 67 |
{pkgbits.V2, pkgbits.DerivedFuncInstance}, |
| 68 |
{pkgbits.V2, pkgbits.DerivedInfoNeeded}, |
| 69 |
{pkgbits.V0, pkgbits.AliasTypeParamNames}, |
| 70 |
{pkgbits.V1, pkgbits.AliasTypeParamNames}, |
| 71 |
} { |
| 72 |
if c.v.Has(c.f) { |
| 73 |
t.Errorf("Expected version %v to not have field %v", c.v, c.f) |
| 74 |
} |
| 75 |
} |
| 76 |
} |