Skip to content

Commit

Permalink
Add tests for Go code
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuens committed Jun 27, 2024
1 parent 41d847b commit dd7f022
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 5 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ The [second implementation](https://pmuens.github.io/time-lock-puzzle/lhtlp.html
1. `git clone <url>`
2. `cd <name>`
3. `go run ./cmd/tlp`
4. `GOOS=js GOARCH=wasm go build -o ./frontend/src/assets/main.wasm cmd/wasm/main.go`
5. `cd frontend`
6. `npm install`
7. `npm run dev`
8. [http://localhost:5173](http://localhost:5173)
4. `go test ./...`
5. `GOOS=js GOARCH=wasm go build -o ./frontend/src/assets/main.wasm cmd/wasm/main.go`
6. `cd frontend`
7. `npm install`
8. `npm run dev`
9. [http://localhost:5173](http://localhost:5173)

## Useful Commands

Expand Down
58 changes: 58 additions & 0 deletions cmd/tlp/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"io"
"testing"
)

type parseConfigTest struct {
name string
args []string
want config
}

func TestParseConfigValidInput(t *testing.T) {
t.Parallel()

for _, tt := range []parseConfigTest{
{
name: "no_flags",
args: []string{},
want: config{bits: 0, message1: 0, message2: 0, difficulty: 100},
},
{
name: "all_flags",
args: []string{"-difficulty=100000"},
want: config{bits: 0, message1: 0, message2: 0, difficulty: 100_000},
},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

var got config
if err := parseConfig(&got, tt.args, io.Discard); err != nil {
t.Fatalf("parseConfig() error = %v, want no error", err)
}
if got != tt.want {
t.Errorf("flags = %+v, want %+v", got, tt.want)
}
})
}
}

func TestParseConfigInvalidInput(t *testing.T) {
t.Parallel()

for _, tt := range []parseConfigTest{
{name: "difficulty_zero", args: []string{"-difficulty=0"}},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := parseConfig(&config{}, tt.args, io.Discard)
if err == nil {
t.Fatal("parseConfig() = nil, want error")
}
})
}
}
2 changes: 2 additions & 0 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build wasm

package main

import (
Expand Down
Binary file modified frontend/src/assets/main.wasm
Binary file not shown.
49 changes: 49 additions & 0 deletions lhtlp/lhtlp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lhtlp_test

import (
"math/big"
"testing"

"github.com/pmuens/time-lock-puzzle/lhtlp"
)

const Bits = 1024

func TestLHTLP(t *testing.T) {
t.Run("Generate and solve one puzzle", func(t *testing.T) {
message := 42
difficulty := 1

want := big.NewInt(int64(message))

params := lhtlp.NewParams(Bits, difficulty)
puzzle := lhtlp.NewPuzzle(*params, message)

got := lhtlp.Solve(*puzzle)

if got.String() != want.String() {
t.Errorf("got %s want %s", got, want)
}
})

t.Run("Generate and solve multiple puzzles", func(t *testing.T) {
message1 := 42
message2 := 24
message3 := 33
difficulty := 1

want := big.NewInt(int64(message1 + message2 + message3))

params := lhtlp.NewParams(Bits, difficulty)
puzzle1 := lhtlp.NewPuzzle(*params, message1)
puzzle2 := lhtlp.NewPuzzle(*params, message2)
puzzle3 := lhtlp.NewPuzzle(*params, message3)
puzzle4 := puzzle1.Add(*puzzle2).Add(*puzzle3)

got := lhtlp.Solve(*puzzle4)

if got.String() != want.String() {
t.Errorf("got %s want %s", got, want)
}
})
}
25 changes: 25 additions & 0 deletions tlp/tlp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tlp_test

import (
"math/big"
"testing"

"github.com/pmuens/time-lock-puzzle/tlp"
)

func TestTLP(t *testing.T) {
t.Run("Generate and solve", func(t *testing.T) {
bits := 1024
message := 42
difficulty := 1

want := big.NewInt(int64(message))

puzzle := tlp.Generate(bits, message, difficulty)
got := tlp.Solve(puzzle)

if got.String() != want.String() {
t.Errorf("got %s want %s", got, want)
}
})
}

0 comments on commit dd7f022

Please sign in to comment.