1// Copyright 2022 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//go:build SELECT_USING_THIS_TAG
6
7package cfile
8
9import "testing"
10
11var funcInvoked bool
12
13//go:noinline
14func thisFunctionOnlyCalledFromSnapshotTest(n int) int {
15	if funcInvoked {
16		panic("bad")
17	}
18	funcInvoked = true
19
20	// Contents here not especially important, just so long as we
21	// have some statements.
22	t := 0
23	for i := 0; i < n; i++ {
24		for j := 0; j < i; j++ {
25			t += i ^ j
26		}
27	}
28	return t
29}
30
31// Tests runtime/coverage.snapshot() directly. Note that if
32// coverage is not enabled, the hook is designed to just return
33// zero.
34func TestCoverageSnapshotImpl(t *testing.T) {
35	C1 := Snapshot()
36	thisFunctionOnlyCalledFromSnapshotTest(15)
37	C2 := Snapshot()
38	cond := "C1 > C2"
39	val := C1 > C2
40	if testing.CoverMode() != "" {
41		cond = "C1 >= C2"
42		val = C1 >= C2
43	}
44	t.Logf("%f %f\n", C1, C2)
45	if val {
46		t.Errorf("erroneous snapshots, %s = true C1=%f C2=%f",
47			cond, C1, C2)
48	}
49}
50