Skip to content

Commit

Permalink
add a silly test example
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Oct 6, 2024
1 parent 202a6da commit 3930d26
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions taskgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package taskgroup_test
import (
"context"
"errors"
"fmt"
"math/rand/v2"
"reflect"
"sync"
Expand Down Expand Up @@ -413,3 +414,47 @@ func (p *peakValue) dec() {
p.cur--
p.μ.Unlock()
}

func TestTree(t *testing.T) {
defer leaktest.Check(t)()

vs := rand.Perm(1000)

g, run := taskgroup.New(nil).Limit(5)

type result [3]int
r := taskgroup.NewRunner[result](run)
r.Collect(func(v result) { t.Logf("+ %d at %d: %d", v[0], v[1], v[2]) })

for i := range vs {
r.Run(func() result {
// Find the location of i in the permutation.
for j, v := range vs {
if v != i {
continue
}

// Count the number of things less than i earlier in vs than i.
// Do this in the most inefficient possible way.
g, run := taskgroup.New(nil).Limit(5)
var countLess int
r := taskgroup.NewRunner[int](run).Collect(func(int) {
countLess++
})

for k := range j {
r.Call(func() (int, error) {
if vs[k] < v {
return k, nil
}
return -1, errors.New("no")
})
}
g.Wait()
return result{i, j, countLess}
}
panic(fmt.Sprintf("%d not found", i))
})
}
g.Wait()
}

0 comments on commit 3930d26

Please sign in to comment.