Skip to content

Commit 3b22e6a

Browse files
committed
test examples
1 parent 38386d0 commit 3b22e6a

File tree

11 files changed

+390
-75
lines changed

11 files changed

+390
-75
lines changed

.cspell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
"lessfunc",
2222
"memprofile",
2323
"pprof",
24+
"Println",
2425
"psilva",
2526
"staticcheck",
27+
"strconv",
2628
"tempfile",
2729
"tempfiles",
2830
"timsort"

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ include version.mk
44

55
ALL_SOURCES := $(shell find . -type f -name '*.go')
66

7-
.PHONY: fmt check test cover coverhtml example
7+
.PHONY: fmt check test cover coverhtml examples
88

99
test:
1010
go test -timeout=90s -v ./...
1111
@echo "< ALL TESTS PASS >"
1212

13-
example:
14-
go run example/example.go >/dev/null
15-
1613
update-deps: go.mod
1714
GOPROXY=direct go get -u ./...
1815
go mod tidy
@@ -37,4 +34,12 @@ check:
3734
staticcheck -checks all ./...
3835

3936
benchmark:
40-
./run_benchmarks.sh
37+
./run_benchmarks.sh
38+
39+
examples:
40+
@for dir in examples/*/; do \
41+
if [ -f "$$dir"*.go ]; then \
42+
echo "Running example in $$dir"; \
43+
(cd "$$dir" && go run *.go > /dev/null); \
44+
fi; \
45+
done

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func main() {
5555

5656
// Process sorted results
5757
for value := range outputChan {
58-
fmt.Println(value)
58+
fmt.pintle(value)
5959
}
6060

6161
// Check for errors
@@ -494,4 +494,4 @@ This project is licensed under the Apache License - see the LICENSE file for det
494494

495495
## Contributing
496496

497-
Contributions are welcome! Please feel free to submit a Pull Request.
497+
Contributions are welcome! Please feel free to submit a Pull Request.

example/example.go

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/gob"
7+
"fmt"
8+
9+
"github.com/lanrat/extsort"
10+
)
11+
12+
type Person struct {
13+
Name string
14+
Age int
15+
}
16+
17+
func personToBytes(p Person) []byte {
18+
var buf bytes.Buffer
19+
enc := gob.NewEncoder(&buf)
20+
enc.Encode(p)
21+
return buf.Bytes()
22+
}
23+
24+
func personFromBytes(data []byte) Person {
25+
var p Person
26+
buf := bytes.NewReader(data)
27+
dec := gob.NewDecoder(buf)
28+
dec.Decode(&p)
29+
return p
30+
}
31+
32+
func comparePersons(a, b Person) bool {
33+
return a.Age < b.Age // Sort by age
34+
}
35+
36+
func main() {
37+
people := []Person{
38+
{"Alice", 30},
39+
{"Bob", 25},
40+
{"Charlie", 35},
41+
}
42+
43+
inputChan := make(chan Person, len(people))
44+
for _, person := range people {
45+
inputChan <- person
46+
}
47+
close(inputChan)
48+
49+
sorter, outputChan, errChan := extsort.Generic(
50+
inputChan,
51+
personFromBytes,
52+
personToBytes,
53+
comparePersons,
54+
nil,
55+
)
56+
57+
go sorter.Sort(context.Background())
58+
59+
fmt.Println("People sorted by age:")
60+
for person := range outputChan {
61+
fmt.Printf("%s (age %d)\n", person.Name, person.Age)
62+
}
63+
64+
if err := <-errChan; err != nil {
65+
panic(err)
66+
}
67+
}

examples/diff/diff.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/lanrat/extsort/diff"
8+
)
9+
10+
func main() {
11+
// Create two sorted string channels
12+
streamA := make(chan string, 5)
13+
streamB := make(chan string, 5)
14+
15+
// Populate stream A
16+
go func() {
17+
defer close(streamA)
18+
for _, item := range []string{"apple", "banana", "cherry", "elderberry"} {
19+
streamA <- item
20+
}
21+
}()
22+
23+
// Populate stream B
24+
go func() {
25+
defer close(streamB)
26+
for _, item := range []string{"banana", "cherry", "date", "fig"} {
27+
streamB <- item
28+
}
29+
}()
30+
31+
// Create error channels
32+
errA := make(chan error, 1)
33+
errB := make(chan error, 1)
34+
close(errA)
35+
close(errB)
36+
37+
// Process differences
38+
result, err := diff.Strings(
39+
context.Background(),
40+
streamA, streamB,
41+
errA, errB,
42+
func(delta diff.Delta, item string) error {
43+
switch delta {
44+
case diff.OLD:
45+
fmt.Printf("Only in A: %s\n", item)
46+
case diff.NEW:
47+
fmt.Printf("Only in B: %s\n", item)
48+
}
49+
return nil
50+
},
51+
)
52+
53+
if err != nil {
54+
panic(err)
55+
}
56+
57+
fmt.Printf("Summary: %d items only in A, %d items only in B, %d common items\n",
58+
result.ExtraA, result.ExtraB, result.Common)
59+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/lanrat/extsort/diff"
8+
)
9+
10+
func main() {
11+
// Create channels with integer data
12+
streamA := make(chan int, 5)
13+
streamB := make(chan int, 5)
14+
errA := make(chan error, 1)
15+
errB := make(chan error, 1)
16+
17+
// Populate streams
18+
go func() {
19+
defer close(streamA)
20+
defer close(errA)
21+
for _, num := range []int{1, 3, 5, 7, 9} {
22+
streamA <- num
23+
}
24+
}()
25+
26+
go func() {
27+
defer close(streamB)
28+
defer close(errB)
29+
for _, num := range []int{2, 4, 5, 6, 8} {
30+
streamB <- num
31+
}
32+
}()
33+
34+
// Compare using generic diff
35+
compareFunc := func(a, b int) int {
36+
if a < b {
37+
return -1
38+
}
39+
if a > b {
40+
return 1
41+
}
42+
return 0
43+
}
44+
45+
resultFunc := func(delta diff.Delta, item int) error {
46+
symbol := map[diff.Delta]string{diff.OLD: "<", diff.NEW: ">"}[delta]
47+
fmt.Printf("%s %d\n", symbol, item)
48+
return nil
49+
}
50+
51+
result, err := diff.Generic(
52+
context.Background(),
53+
streamA, streamB,
54+
errA, errB,
55+
compareFunc,
56+
resultFunc,
57+
)
58+
59+
if err != nil {
60+
panic(err)
61+
}
62+
63+
fmt.Printf("Differences found: %d\n", result.ExtraA+result.ExtraB)
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"sync"
7+
8+
"github.com/lanrat/extsort/diff"
9+
)
10+
11+
func main() {
12+
streamA := make(chan string, 100)
13+
streamB := make(chan string, 100)
14+
errA := make(chan error, 1)
15+
errB := make(chan error, 1)
16+
17+
// Populate streams with test data
18+
go func() {
19+
defer close(streamA)
20+
defer close(errA)
21+
for i := 0; i < 50; i += 2 {
22+
streamA <- fmt.Sprintf("item_%03d", i)
23+
}
24+
}()
25+
26+
go func() {
27+
defer close(streamB)
28+
defer close(errB)
29+
for i := 1; i < 50; i += 2 {
30+
streamB <- fmt.Sprintf("item_%03d", i)
31+
}
32+
}()
33+
34+
// Use channel-based result processing for parallel handling
35+
resultFunc, resultChan := diff.StringResultChan()
36+
37+
var wg sync.WaitGroup
38+
wg.Add(1)
39+
40+
// Process results in parallel
41+
go func() {
42+
defer wg.Done()
43+
for result := range resultChan {
44+
fmt.Printf("Difference: %s %s\n", result.D, result.S)
45+
}
46+
}()
47+
48+
// Start diff operation
49+
go func() {
50+
defer close(resultChan)
51+
_, err := diff.Strings(
52+
context.Background(),
53+
streamA, streamB,
54+
errA, errB,
55+
resultFunc,
56+
)
57+
if err != nil {
58+
fmt.Printf("Diff error: %v\n", err)
59+
}
60+
}()
61+
62+
wg.Wait()
63+
fmt.Println("Diff processing complete")
64+
}

0 commit comments

Comments
 (0)