File size: 1,969 Bytes
e36aeda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pkgbits_test

import (
	"internal/pkgbits"
	"strings"
	"testing"
)

func TestRoundTrip(t *testing.T) {
	for _, version := range []pkgbits.Version{
		pkgbits.V0,
		pkgbits.V1,
		pkgbits.V2,
	} {
		pw := pkgbits.NewPkgEncoder(version, -1)
		w := pw.NewEncoder(pkgbits.SectionMeta, pkgbits.SyncPublic)
		w.Flush()

		var b strings.Builder
		_ = pw.DumpTo(&b)
		input := b.String()

		pr := pkgbits.NewPkgDecoder("package_id", input)
		r := pr.NewDecoder(pkgbits.SectionMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)

		if r.Version() != w.Version() {
			t.Errorf("Expected reader version %d to be the writer version %d", r.Version(), w.Version())
		}
	}
}

// Type checker to enforce that know V* have the constant values they must have.
var _ [0]bool = [pkgbits.V0]bool{}
var _ [1]bool = [pkgbits.V1]bool{}

func TestVersions(t *testing.T) {
	type vfpair struct {
		v pkgbits.Version
		f pkgbits.Field
	}

	// has field tests
	for _, c := range []vfpair{
		{pkgbits.V1, pkgbits.Flags},
		{pkgbits.V2, pkgbits.Flags},
		{pkgbits.V0, pkgbits.HasInit},
		{pkgbits.V1, pkgbits.HasInit},
		{pkgbits.V0, pkgbits.DerivedFuncInstance},
		{pkgbits.V1, pkgbits.DerivedFuncInstance},
		{pkgbits.V0, pkgbits.DerivedInfoNeeded},
		{pkgbits.V1, pkgbits.DerivedInfoNeeded},
		{pkgbits.V2, pkgbits.AliasTypeParamNames},
	} {
		if !c.v.Has(c.f) {
			t.Errorf("Expected version %v to have field %v", c.v, c.f)
		}
	}

	// does not have field tests
	for _, c := range []vfpair{
		{pkgbits.V0, pkgbits.Flags},
		{pkgbits.V2, pkgbits.HasInit},
		{pkgbits.V2, pkgbits.DerivedFuncInstance},
		{pkgbits.V2, pkgbits.DerivedInfoNeeded},
		{pkgbits.V0, pkgbits.AliasTypeParamNames},
		{pkgbits.V1, pkgbits.AliasTypeParamNames},
	} {
		if c.v.Has(c.f) {
			t.Errorf("Expected version %v to not have field %v", c.v, c.f)
		}
	}
}