Skip to content

Commit

Permalink
unicode/norm: look for symbols in TestLinking
Browse files Browse the repository at this point in the history
Previously, we just inspected sizes to tell if the linker was stripping
unnecessary bits, so, actually look for symbols.

Fixes golang/go#34209

Change-Id: Iec355ac46a967aeca9c0c1a950ead6b563c5e84e
Reviewed-on: https://go-review.googlesource.com/c/text/+/221104
Run-TryBot: Jeremy Faller <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
jeremyfaller committed Mar 6, 2020
1 parent 929e72c commit 06d492a
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions unicode/norm/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"unicode/utf8"
Expand Down Expand Up @@ -920,22 +925,53 @@ func TestString(t *testing.T) {
})
}

func runNM(code string) (string, error) {
// Write the file.
tmpdir, err := ioutil.TempDir(os.TempDir(), "normalize_test")
if err != nil {
return "", fmt.Errorf("failed to create tmpdir: %v", err)
}
defer os.RemoveAll(tmpdir)
goTool := filepath.Join(runtime.GOROOT(), "bin", "go")
filename := filepath.Join(tmpdir, "main.go")
if err := ioutil.WriteFile(filename, []byte(code), 0644); err != nil {
return "", fmt.Errorf("failed to write main.go: %v", err)
}
outputFile := filepath.Join(tmpdir, "main")

// Build the binary.
out, err := exec.Command(goTool, "build", "-o", outputFile, filename).CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to execute command: %v", err)
}

// Get the symbols.
out, err = exec.Command(goTool, "tool", "nm", outputFile).CombinedOutput()
return string(out), err
}

func TestLinking(t *testing.T) {
const prog = `
package main
import "fmt"
import "golang.org/x/text/unicode/norm"
func main() { fmt.Println(norm.%s) }
`
baseline, errB := testtext.CodeSize(fmt.Sprintf(prog, "MaxSegmentSize"))
withTables, errT := testtext.CodeSize(fmt.Sprintf(prog, `NFC.String("")`))

baseline, errB := runNM(fmt.Sprintf(prog, "MaxSegmentSize"))
withTables, errT := runNM(fmt.Sprintf(prog, `NFC.String("")`))
if errB != nil || errT != nil {
t.Skipf("code size failed: %v and %v", errB, errT)
t.Skipf("TestLinking failed: %v and %v", errB, errT)
}
// Tables are at least 50K
if d := withTables - baseline; d < 50*1024 {
t.Errorf("tables appear not to be dropped: %d - %d = %d",
withTables, baseline, d)

symbols := []string{"norm.formTable", "norm.nfkcValues", "norm.decomps"}
for _, symbol := range symbols {
if strings.Contains(baseline, symbol) {
t.Errorf("found: %q unexpectedly", symbol)
}
if !strings.Contains(withTables, symbol) {
t.Errorf("didn't find: %q unexpectedly", symbol)
}
}
}

Expand Down

0 comments on commit 06d492a

Please sign in to comment.