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