...
Run Format

Source file src/cmd/internal/src/xpos_test.go

Documentation: cmd/internal/src

     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)
    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  
    41  	if len(tab.baseList) != len(tab.indexMap) {
    42  		t.Errorf("table length discrepancy: %d != %d", len(tab.baseList), len(tab.indexMap))
    43  	}
    44  
    45  	const wantLen = 4
    46  	if len(tab.baseList) != wantLen {
    47  		t.Errorf("got table length %d; want %d", len(tab.baseList), wantLen)
    48  	}
    49  
    50  	if got := tab.XPos(NoPos); got != NoXPos {
    51  		t.Errorf("XPos(NoPos): got %v; want %v", got, NoXPos)
    52  	}
    53  
    54  	if tab.baseList[0] != nil || tab.indexMap[nil] != 0 {
    55  		t.Errorf("nil base not at index 0")
    56  	}
    57  }
    58  
    59  func TestSize(t *testing.T) {
    60  	var p XPos
    61  	if unsafe.Alignof(p) != 4 {
    62  		t.Errorf("alignment = %v; want 4", unsafe.Alignof(p))
    63  	}
    64  	if unsafe.Sizeof(p) != 8 {
    65  		t.Errorf("size = %v; want 8", unsafe.Sizeof(p))
    66  	}
    67  }
    68  
    69  func TestSetBase(t *testing.T) {
    70  	var tab PosTable
    71  	b1 := NewFileBase("b1", "b1")
    72  	orig := MakePos(b1, 42, 7)
    73  	xpos := tab.XPos(orig)
    74  
    75  	pos := tab.Pos(xpos)
    76  	new := NewInliningBase(b1, 2)
    77  	pos.SetBase(new)
    78  	xpos = tab.XPos(pos)
    79  
    80  	pos = tab.Pos(xpos)
    81  	if inl := pos.Base().InliningIndex(); inl != 2 {
    82  		t.Fatalf("wrong inlining index: %d", inl)
    83  	}
    84  }
    85  

View as plain text