From a1558dcce36b2d46cc37c11a1b65c918471c0dbb Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Mon, 13 May 2024 13:54:31 +0530 Subject: [PATCH] Add remaining --- main.go | 7 +++-- spawn/spawn.go | 55 +++++++++++++++++++++++++++++++++++++ testbench/testbench.go | 9 ++++++ testbench/testbench_test.go | 46 +++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 testbench/testbench_test.go diff --git a/main.go b/main.go index 313ab85..ec691a1 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,9 @@ import ( // "git.sangeeth.dev/gobyexample/httpserver" // "git.sangeeth.dev/gobyexample/httpctx" // "git.sangeeth.dev/gobyexample/exec" - "git.sangeeth.dev/gobyexample/signals" + // "git.sangeeth.dev/gobyexample/signals" + "git.sangeeth.dev/gobyexample/spawn" + _ "git.sangeeth.dev/gobyexample/testbench" ) func main() { @@ -103,5 +105,6 @@ func main() { // httpserver.HttpServer() // httpctx.HttpCtx() // exec.Exec() - signals.Signals() + // signals.Signals() + spawn.Spawn() } diff --git a/spawn/spawn.go b/spawn/spawn.go index e69de29..ddce2c7 100644 --- a/spawn/spawn.go +++ b/spawn/spawn.go @@ -0,0 +1,55 @@ +package spawn + +import ( + "fmt" + "io" + "os/exec" +) + +func Spawn() { + // Cmd object represents the external process + dateCmd := exec.Command("date") + + // Runs and wait for it to finish, output collected + dateOut, err := dateCmd.Output() + + if err != nil { + panic(err) + } + + fmt.Println("> date") + fmt.Println(string(dateOut)) + + _, err = exec.Command("date", "-x").Output() + if err != nil { + switch e := err.(type) { + case *exec.Error: + fmt.Println("failed executing:", err) + case *exec.ExitError: + fmt.Println("command exit code:", e.ExitCode()) + default: + panic(err) + } + } + + grepCmd := exec.Command("grep", "hello") + + grepIn, _ := grepCmd.StdinPipe() + grepOut, _ := grepCmd.StdoutPipe() + grepCmd.Start() + grepIn.Write([]byte("hello world\ngoodbye grep\nI am Jin, hello")) + grepIn.Close() + grepBytes, _ := io.ReadAll(grepOut) + grepCmd.Wait() + + fmt.Println("> grep hello") + fmt.Println(string(grepBytes)) + + lsCmd := exec.Command("bash", "-c", "ls -a -l -h") + lsOut, err := lsCmd.Output() + if err != nil { + panic(err) + } + fmt.Println("> ls -a -l -h") + fmt.Println(string(lsOut)) +} diff --git a/testbench/testbench.go b/testbench/testbench.go index e69de29..86f2fa5 100644 --- a/testbench/testbench.go +++ b/testbench/testbench.go @@ -0,0 +1,9 @@ +package testbench + +func IntMin(a, b int) int { + if a < b { + return a + } + + return b +} diff --git a/testbench/testbench_test.go b/testbench/testbench_test.go new file mode 100644 index 0000000..725441b --- /dev/null +++ b/testbench/testbench_test.go @@ -0,0 +1,46 @@ +package testbench + +import ( + "fmt" + "testing" +) + +func TestIntMinBasic(t *testing.T) { + ans := IntMin(2, -2) + + if ans != -2 { + t.Errorf("IntMain(2, -2) = %d, want -2", ans) + } +} + +func TestIntMinTableDrive(t *testing.T) { + var tests = []struct { + a, b int + want int + }{ + {0, 1, 0}, + {1, 0, 0}, + {2, -2, -2}, + {0, -1, -1}, + {-1, 0, -1}, + } + + for _, tt := range tests { + testName := fmt.Sprintf("%d,%d", tt.a, tt.b) + t.Run(testName, func(t *testing.T) { + ans := IntMin(tt.a, tt.b) + if ans != tt.want { + t.Errorf("got %d, want %d", ans, tt.want) + } + }) + } +} + +func BenchmarkIntMin(b *testing.B) { + for i := 0; i < b.N; i++ { + IntMin(1, 2) + } +} + +// go test -v # to run all tests in current project in verbose mode +// go test -bench # to run all benchmarks in current project