Source file src/internal/pkgbits/pkgbits_test.go

     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  		pkgbits.V3,
    19  	} {
    20  		pw := pkgbits.NewPkgEncoder(version, -1)
    21  		w := pw.NewEncoder(pkgbits.SectionMeta, 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.SectionMeta, 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 known V* have the constant values they must have.
    38  var (
    39  	_ [0]bool = [pkgbits.V0]bool{}
    40  	_ [1]bool = [pkgbits.V1]bool{}
    41  	_ [2]bool = [pkgbits.V2]bool{}
    42  	_ [3]bool = [pkgbits.V3]bool{}
    43  )
    44  
    45  func TestVersions(t *testing.T) {
    46  	type vfpair struct {
    47  		v pkgbits.Version
    48  		f pkgbits.Field
    49  	}
    50  
    51  	// has field tests
    52  	for _, c := range []vfpair{
    53  		{pkgbits.V1, pkgbits.Flags},
    54  		{pkgbits.V2, pkgbits.Flags},
    55  		{pkgbits.V0, pkgbits.HasInit},
    56  		{pkgbits.V1, pkgbits.HasInit},
    57  		{pkgbits.V0, pkgbits.DerivedFuncInstance},
    58  		{pkgbits.V1, pkgbits.DerivedFuncInstance},
    59  		{pkgbits.V0, pkgbits.DerivedInfoNeeded},
    60  		{pkgbits.V1, pkgbits.DerivedInfoNeeded},
    61  		{pkgbits.V2, pkgbits.AliasTypeParamNames},
    62  		{pkgbits.V3, pkgbits.CompactCompLiterals},
    63  	} {
    64  		if !c.v.Has(c.f) {
    65  			t.Errorf("Expected version %v to have field %v", c.v, c.f)
    66  		}
    67  	}
    68  
    69  	// does not have field tests
    70  	for _, c := range []vfpair{
    71  		{pkgbits.V0, pkgbits.Flags},
    72  		{pkgbits.V2, pkgbits.HasInit},
    73  		{pkgbits.V2, pkgbits.DerivedFuncInstance},
    74  		{pkgbits.V2, pkgbits.DerivedInfoNeeded},
    75  		{pkgbits.V0, pkgbits.AliasTypeParamNames},
    76  		{pkgbits.V1, pkgbits.AliasTypeParamNames},
    77  		{pkgbits.V0, pkgbits.CompactCompLiterals},
    78  		{pkgbits.V1, pkgbits.CompactCompLiterals},
    79  		{pkgbits.V2, pkgbits.CompactCompLiterals},
    80  	} {
    81  		if c.v.Has(c.f) {
    82  			t.Errorf("Expected version %v to not have field %v", c.v, c.f)
    83  		}
    84  	}
    85  }
    86  

View as plain text