aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/asm/internal/asm/endtoend_test.go
blob: 5ee6e80d2bfbc140824c84391b3c648a02ef3a60 (plain)
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
78
79
// Copyright 2015 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 asm

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"cmd/asm/internal/lex"
	"cmd/internal/obj"
)

// An end-to-end test for the assembler: Do we print what we parse?
// Output is generated by, in effect, turning on -S and comparing the
// result against a golden file.

func testEndToEnd(t *testing.T, goarch string) {
	lex.InitHist()
	input := filepath.Join("testdata", goarch+".s")
	output := filepath.Join("testdata", goarch+".out")
	architecture, ctxt := setArch(goarch)
	lexer := lex.NewLexer(input, ctxt)
	parser := NewParser(ctxt, architecture, lexer)
	pList := obj.Linknewplist(ctxt)
	var ok bool
	testOut = new(bytes.Buffer) // The assembler writes -S output to this buffer.
	ctxt.Bso = obj.Binitw(os.Stdout)
	defer obj.Bflush(ctxt.Bso)
	ctxt.Diag = log.Fatalf
	obj.Binitw(ioutil.Discard)
	pList.Firstpc, ok = parser.Parse()
	if !ok {
		t.Fatalf("asm: ppc64 assembly failed")
	}
	result := string(testOut.Bytes())
	expect, err := ioutil.ReadFile(output)
	// For Windows.
	result = strings.Replace(result, `testdata\`, `testdata/`, -1)
	if err != nil {
		t.Fatal(err)
	}
	if result != string(expect) {
		if false { // Enable to capture output.
			fmt.Printf("%s", result)
			os.Exit(1)
		}
		t.Errorf("%s failed: output differs", goarch)
		r := strings.Split(result, "\n")
		e := strings.Split(string(expect), "\n")
		if len(r) != len(e) {
			t.Errorf("%s: expected %d lines, got %d", len(e), len(r))
		}
		n := len(e)
		if n > len(r) {
			n = len(r)
		}
		for i := 0; i < n; i++ {
			if r[i] != e[i] {
				t.Errorf("%s:%d:\nexpected\n\t%s\ngot\n\t%s", output, i, e[i], r[i])
			}
		}
	}
}

func TestPPC64EndToEnd(t *testing.T) {
	testEndToEnd(t, "ppc64")
}

func TestARMEndToEnd(t *testing.T) {
	testEndToEnd(t, "arm")
}