github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/internal/src/xpos_test.go (about)

     1  // Copyright 2016 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 src
     6  
     7  import (
     8  	"testing"
     9  	"unsafe"
    10  )
    11  
    12  func TestNoXPos(t *testing.T) {
    13  	var tab PosTable
    14  	if tab.Pos(NoXPos) != NoPos {
    15  		t.Errorf("failed to translate NoXPos to Pos using zero PosTable")
    16  	}
    17  }
    18  
    19  func TestConversion(t *testing.T) {
    20  	b1 := NewFileBase("b1", "b1")
    21  	b2 := NewFileBase("b2", "b2")
    22  	b3 := NewLinePragmaBase(MakePos(b1, 10, 0), "b3", "b3", 123, 0)
    23  
    24  	var tab PosTable
    25  	for _, want := range []Pos{
    26  		NoPos,
    27  		MakePos(nil, 0, 0), // same table entry as NoPos
    28  		MakePos(b1, 0, 0),
    29  		MakePos(nil, 10, 20), // same table entry as NoPos
    30  		MakePos(b2, 10, 20),
    31  		MakePos(b3, 10, 20),
    32  		MakePos(b3, 123, 0), // same table entry as MakePos(b3, 10, 20)
    33  	} {
    34  		xpos := tab.XPos(want)
    35  		got := tab.Pos(xpos)
    36  		if got != want {
    37  			t.Errorf("got %v; want %v", got, want)
    38  		}
    39  
    40  		for _, x := range []struct {
    41  			f func(XPos) XPos
    42  			e uint
    43  		}{
    44  			{XPos.WithDefaultStmt, PosDefaultStmt},
    45  			{XPos.WithIsStmt, PosIsStmt},
    46  			{XPos.WithNotStmt, PosNotStmt},
    47  			{XPos.WithIsStmt, PosIsStmt},
    48  			{XPos.WithDefaultStmt, PosDefaultStmt},
    49  			{XPos.WithNotStmt, PosNotStmt}} {
    50  			xposWith := x.f(xpos)
    51  			expected := x.e
    52  			if xpos.Line() == 0 && xpos.Col() == 0 {
    53  				expected = PosNotStmt
    54  			}
    55  			if got := xposWith.IsStmt(); got != expected {
    56  				t.Errorf("expected %v; got %v", expected, got)
    57  			}
    58  			if xposWith.Col() != xpos.Col() || xposWith.Line() != xpos.Line() {
    59  				t.Errorf("line:col, before = %d:%d, after=%d:%d", xpos.Line(), xpos.Col(), xposWith.Line(), xposWith.Col())
    60  			}
    61  			xpos = xposWith
    62  		}
    63  	}
    64  
    65  	if len(tab.baseList) != len(tab.indexMap) {
    66  		t.Errorf("table length discrepancy: %d != %d", len(tab.baseList), len(tab.indexMap))
    67  	}
    68  
    69  	const wantLen = 4
    70  	if len(tab.baseList) != wantLen {
    71  		t.Errorf("got table length %d; want %d", len(tab.baseList), wantLen)
    72  	}
    73  
    74  	if got := tab.XPos(NoPos); got != NoXPos {
    75  		t.Errorf("XPos(NoPos): got %v; want %v", got, NoXPos)
    76  	}
    77  
    78  	if tab.baseList[0] != nil || tab.indexMap[nil] != 0 {
    79  		t.Errorf("nil base not at index 0")
    80  	}
    81  }
    82  
    83  func TestSize(t *testing.T) {
    84  	var p XPos
    85  	if unsafe.Alignof(p) != 4 {
    86  		t.Errorf("alignment = %v; want 4", unsafe.Alignof(p))
    87  	}
    88  	if unsafe.Sizeof(p) != 8 {
    89  		t.Errorf("size = %v; want 8", unsafe.Sizeof(p))
    90  	}
    91  }
    92  
    93  func TestSetBase(t *testing.T) {
    94  	var tab PosTable
    95  	b1 := NewFileBase("b1", "b1")
    96  	orig := MakePos(b1, 42, 7)
    97  	xpos := tab.XPos(orig)
    98  
    99  	pos := tab.Pos(xpos)
   100  	new := NewInliningBase(b1, 2)
   101  	pos.SetBase(new)
   102  	xpos = tab.XPos(pos)
   103  
   104  	pos = tab.Pos(xpos)
   105  	if inl := pos.Base().InliningIndex(); inl != 2 {
   106  		t.Fatalf("wrong inlining index: %d", inl)
   107  	}
   108  }