Files
print_hej/go/src/pi_test.go
T
Ein Anderssono 54d2fecee0 Initial commit: Pi calculation benchmark with 34 languages
- Added implementations for: bash, brainfuck, c, cpp, crystal, csharp, d, dart, elixir, erlang, fortran, go, haskell, java, javascript, julia, kotlin, objective-c, scala, typescript, lua, nim, odin, perl, php, python, r, ruby, rust, swift, zig, assembly, vimscript, wolfram
- All implementations use Machin's formula: π/4 = 4*arctan(1/5) - arctan(1/239)
- Build system with ./build.sh, test system with ./test.sh
- Performance testing with ./run_all.sh
- Comprehensive README.md explaining performance differences
- Test framework verifies correctness against known π values
2026-04-23 00:26:18 +02:00

73 lines
1.7 KiB
Go

package main
import (
"os/exec"
"strings"
"testing"
)
const scriptPath = "/Users/einand/Code/test/go/print_hej"
func runScript(decimals ...int) string {
args := []string{}
if len(decimals) > 0 {
args = append(args, string(rune(decimals[0])))
}
cmd := exec.Command(scriptPath, args...)
output, err := cmd.Output()
if err != nil {
panic(err)
}
return strings.TrimSpace(string(output))
}
func Test10Decimals(t *testing.T) {
result := runScript(10)
expected := "3.1415926535"
if result != expected {
t.Errorf("Expected %s, got %s", expected, result)
}
}
func Test5Decimals(t *testing.T) {
result := runScript(5)
expected := "3.14159"
if result != expected {
t.Errorf("Expected %s, got %s", expected, result)
}
}
func Test1Decimal(t *testing.T) {
result := runScript(1)
expected := "3.1"
if result != expected {
t.Errorf("Expected %s, got %s", expected, result)
}
}
func Test100Decimals(t *testing.T) {
result := runScript(100)
expected := "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
if result != expected {
t.Errorf("Expected %s, got %s", expected, result)
}
}
func TestDefault100Decimals(t *testing.T) {
result := runScript()
expected := "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
if result != expected {
t.Errorf("Expected %s, got %s", expected, result)
}
}
func Test10000Decimals(t *testing.T) {
result := runScript(10000)
// Check length: "3." + 10000 digits = 10002 characters
if len(result) != 10002 {
t.Errorf("Expected 10002 characters, got %d", len(result))
}
if !strings.HasPrefix(result, "3.14159") {
t.Errorf("Result should start with 3.14159, got %s", result[:10])
}
}