summaryrefslogtreecommitdiffstats
path: root/src/embed/internal/embedtest/embedx_test.go
blob: 27fa11614e9268ea19149cb5c4cabd9faa374407 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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 embedtest_test

import (
	"embed"
	"os"
	"testing"
)

var (
	global2      = global
	concurrency2 = concurrency
	glass2       = glass
	sbig2        = sbig
	bbig2        = bbig
)

//go:embed testdata/*.txt
var global embed.FS

//go:embed c*txt
var concurrency string

//go:embed testdata/g*.txt
var glass []byte

//go:embed testdata/ascii.txt
var sbig string

//go:embed testdata/ascii.txt
var bbig []byte

func testFiles(t *testing.T, f embed.FS, name, data string) {
	t.Helper()
	d, err := f.ReadFile(name)
	if err != nil {
		t.Error(err)
		return
	}
	if string(d) != data {
		t.Errorf("read %v = %q, want %q", name, d, data)
	}
}

func testString(t *testing.T, s, name, data string) {
	t.Helper()
	if s != data {
		t.Errorf("%v = %q, want %q", name, s, data)
	}
}

func TestXGlobal(t *testing.T) {
	testFiles(t, global, "testdata/hello.txt", "hello, world\n")
	testString(t, concurrency, "concurrency", "Concurrency is not parallelism.\n")
	testString(t, string(glass), "glass", "I can eat glass and it doesn't hurt me.\n")
	testString(t, concurrency2, "concurrency2", "Concurrency is not parallelism.\n")
	testString(t, string(glass2), "glass2", "I can eat glass and it doesn't hurt me.\n")

	big, err := os.ReadFile("testdata/ascii.txt")
	if err != nil {
		t.Fatal(err)
	}
	testString(t, sbig, "sbig", string(big))
	testString(t, sbig2, "sbig2", string(big))
	testString(t, string(bbig), "bbig", string(big))
	testString(t, string(bbig2), "bbig", string(big))

	if t.Failed() {
		return
	}

	// Could check &glass[0] == &glass2[0] but also want to make sure write does not fault
	// (data must not be in read-only memory).
	old := glass[0]
	glass[0]++
	if glass2[0] != glass[0] {
		t.Fatalf("glass and glass2 do not share storage")
	}
	glass[0] = old

	// Could check &bbig[0] == &bbig2[0] but also want to make sure write does not fault
	// (data must not be in read-only memory).
	old = bbig[0]
	bbig[0]++
	if bbig2[0] != bbig[0] {
		t.Fatalf("bbig and bbig2 do not share storage")
	}
	bbig[0] = old
}