aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/goobj2/objfile_test.go
blob: ee15136cbe3df6db0bde63a047fdf7440a898bca (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
// Copyright 2020 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 goobj2

import (
	"bufio"
	"bytes"
	"cmd/internal/bio"
	"testing"
)

func dummyWriter() *Writer {
	var buf bytes.Buffer
	wr := &bio.Writer{Writer: bufio.NewWriter(&buf)} // hacky: no file, so cannot seek
	return NewWriter(wr)
}

func TestSize(t *testing.T) {
	// This test checks that hard-coded sizes match the actual sizes
	// in the object file format.
	tests := []struct {
		x    interface{ Write(*Writer) }
		want uint32
	}{
		{&Reloc{}, RelocSize},
		{&Aux{}, AuxSize},
	}
	w := dummyWriter()
	for _, test := range tests {
		off0 := w.off
		test.x.Write(w)
		got := w.off - off0
		if got != test.want {
			t.Errorf("size(%T) mismatch: %d bytes written, but size=%d", test.x, got, test.want)
		}
	}
}