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  		pkgbits.V4,
    20  		pkgbits.V5,
    21  	} {
    22  		pw := pkgbits.NewPkgEncoder(version, -1)
    23  		w := pw.NewEncoder(pkgbits.SectionMeta, pkgbits.SyncPublic)
    24  		w.Flush()
    25  
    26  		var b strings.Builder
    27  		_ = pw.DumpTo(&b)
    28  		input := b.String()
    29  
    30  		pr := pkgbits.NewPkgDecoder("package_id", input)
    31  		r := pr.NewDecoder(pkgbits.SectionMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)
    32  
    33  		if r.Version() != w.Version() {
    34  			t.Errorf("Expected reader version %d to be the writer version %d", r.Version(), w.Version())
    35  		}
    36  	}
    37  }
    38  
    39  // Type checker to enforce that known V* have the constant values they must have.
    40  var (
    41  	_ [0]bool = [pkgbits.V0]bool{}
    42  	_ [1]bool = [pkgbits.V1]bool{}
    43  	_ [2]bool = [pkgbits.V2]bool{}
    44  	_ [3]bool = [pkgbits.V3]bool{}
    45  	_ [4]bool = [pkgbits.V4]bool{}
    46  	_ [5]bool = [pkgbits.V5]bool{}
    47  )
    48  
    49  func TestVersions(t *testing.T) {
    50  	type vfpair struct {
    51  		v pkgbits.Version
    52  		f pkgbits.Field
    53  	}
    54  
    55  	// has field tests
    56  	for _, c := range []vfpair{
    57  		{pkgbits.V1, pkgbits.Flags},
    58  		{pkgbits.V2, pkgbits.Flags},
    59  		{pkgbits.V0, pkgbits.HasInit},
    60  		{pkgbits.V1, pkgbits.HasInit},
    61  		{pkgbits.V0, pkgbits.DerivedFuncInstance},
    62  		{pkgbits.V1, pkgbits.DerivedFuncInstance},
    63  		{pkgbits.V0, pkgbits.DerivedInfoNeeded},
    64  		{pkgbits.V1, pkgbits.DerivedInfoNeeded},
    65  		{pkgbits.V2, pkgbits.AliasTypeParamNames},
    66  		{pkgbits.V3, pkgbits.CompactCompLiterals},
    67  		{pkgbits.V4, pkgbits.GenericMethods},
    68  		{pkgbits.V5, pkgbits.PreserveMethodOrder},
    69  	} {
    70  		if !c.v.Has(c.f) {
    71  			t.Errorf("Expected version %v to have field %v", c.v, c.f)
    72  		}
    73  	}
    74  
    75  	// does not have field tests
    76  	for _, c := range []vfpair{
    77  		{pkgbits.V0, pkgbits.Flags},
    78  		{pkgbits.V2, pkgbits.HasInit},
    79  		{pkgbits.V2, pkgbits.DerivedFuncInstance},
    80  		{pkgbits.V2, pkgbits.DerivedInfoNeeded},
    81  		{pkgbits.V0, pkgbits.AliasTypeParamNames},
    82  		{pkgbits.V1, pkgbits.AliasTypeParamNames},
    83  		{pkgbits.V0, pkgbits.CompactCompLiterals},
    84  		{pkgbits.V1, pkgbits.CompactCompLiterals},
    85  		{pkgbits.V2, pkgbits.CompactCompLiterals},
    86  		{pkgbits.V0, pkgbits.GenericMethods},
    87  		{pkgbits.V1, pkgbits.GenericMethods},
    88  		{pkgbits.V2, pkgbits.GenericMethods},
    89  		{pkgbits.V3, pkgbits.GenericMethods},
    90  		{pkgbits.V0, pkgbits.PreserveMethodOrder},
    91  		{pkgbits.V1, pkgbits.PreserveMethodOrder},
    92  		{pkgbits.V2, pkgbits.PreserveMethodOrder},
    93  		{pkgbits.V3, pkgbits.PreserveMethodOrder},
    94  		{pkgbits.V4, pkgbits.PreserveMethodOrder},
    95  	} {
    96  		if c.v.Has(c.f) {
    97  			t.Errorf("Expected version %v to not have field %v", c.v, c.f)
    98  		}
    99  	}
   100  }
   101  

View as plain text