Add remaining

This commit is contained in:
Sangeeth Sudheer 2024-05-13 13:54:31 +05:30
parent 2dbd7ac062
commit a1558dcce3
Signed by: x
GPG Key ID: F6D06ECE734C57D1
4 changed files with 115 additions and 2 deletions

View File

@ -50,7 +50,9 @@ import (
// "git.sangeeth.dev/gobyexample/httpserver" // "git.sangeeth.dev/gobyexample/httpserver"
// "git.sangeeth.dev/gobyexample/httpctx" // "git.sangeeth.dev/gobyexample/httpctx"
// "git.sangeeth.dev/gobyexample/exec" // "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() { func main() {
@ -103,5 +105,6 @@ func main() {
// httpserver.HttpServer() // httpserver.HttpServer()
// httpctx.HttpCtx() // httpctx.HttpCtx()
// exec.Exec() // exec.Exec()
signals.Signals() // signals.Signals()
spawn.Spawn()
} }

View File

@ -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))
}

View File

@ -0,0 +1,9 @@
package testbench
func IntMin(a, b int) int {
if a < b {
return a
}
return b
}

View File

@ -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