Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add complex integration test
  • Loading branch information
grantnelson-wf committed Aug 15, 2025
commit 99b9ba601089c9417bfff6b4a1f3f45ec9be3dfe
2 changes: 2 additions & 0 deletions tests/gencircle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func Test_GenCircle_Burninate(t *testing.T) { runGenCircleTest(t, `burninate`) }

func Test_GenCircle_CatBox(t *testing.T) { runGenCircleTest(t, `catbox`) }

func Test_GenCircle_Trammel(t *testing.T) { runGenCircleTest(t, `trammel`) }

// Cache buster: Keeping the tests from using cached results when only
// the test application files are changed.
//
Expand Down
10 changes: 10 additions & 0 deletions tests/testdata/gencircle/trammel/cmp/cmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cmp

// Barrowed from the cmp package in the Go standard library
// that isn't available until go1.21.
type Ordered interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64 |
~string
}
31 changes: 31 additions & 0 deletions tests/testdata/gencircle/trammel/collections/collections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package collections

func Populate[K comparable, V any, SK ~[]K, SV ~[]V, M ~map[K]V](m M, keys SK, values SV) {
// Lots of type parameters with parameters referencing each other.
for i, k := range keys {
if i < len(values) {
m[k] = values[i]
} else {
var zero V
m[k] = zero
}
}
}

func KeysAndValues[K comparable, V any, M ~map[K]V](m M) struct {
Keys []K
Values []V
} {
keys := make([]K, 0, len(m))
values := make([]V, 0, len(m))
for k, v := range m {
keys = append(keys, k)
values = append(values, v)
}
// nested generic type that has a type parameter and nest type parameter.
type result[T any] struct {
Keys []T
Values []V
}
return result[K]{Keys: keys, Values: values}
}
38 changes: 38 additions & 0 deletions tests/testdata/gencircle/trammel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// This is a test of several different kinds of generics.
// It is purposefully overly complecated for testing purposes.
// This integration test is similar to `compiler.Test_CrossPackageAnalysis`.

package main

import (
"fmt"

"github.com/gopherjs/gopherjs/tests/testdata/gencircle/trammel/cmp"
"github.com/gopherjs/gopherjs/tests/testdata/gencircle/trammel/collections"
"github.com/gopherjs/gopherjs/tests/testdata/gencircle/trammel/stable"
)

type StableMap[K cmp.Ordered, V any] map[K]V

func (m StableMap[K, V]) String() string {
return stable.MapString(m, func(k K, v V) string {
return fmt.Sprintf(`%v: %v`, k, v)
})
}

type SIMap = StableMap[string, int]
type ISMap = StableMap[int, string]

func main() {
m1 := SIMap{}
collections.Populate(m1,
[]string{"one", "two", "three"},
[]int{1, 2, 3})
println(m1.String())

m2 := ISMap{}
collections.Populate(m2,
[]int{4, 5, 6},
[]string{"four", "five", "six"})
println(m2.String())
}
2 changes: 2 additions & 0 deletions tests/testdata/gencircle/trammel/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{one: 1, three: 3, two: 2}
{4: four, 5: five, 6: six}
30 changes: 30 additions & 0 deletions tests/testdata/gencircle/trammel/sorts/sorts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sorts

import "github.com/gopherjs/gopherjs/tests/testdata/gencircle/trammel/cmp"

func Pair[K cmp.Ordered, V any, SK ~[]K, SV ~[]V](k SK, v SV) {
Bubble(struct { // non-generic struct in a generic context.
len func() int
less func(i, j int) bool
swap func(i, j int)
}{
len: func() int { return len(k) },
less: func(i, j int) bool { return k[i] < k[j] },
swap: func(i, j int) { k[i], v[i], k[j], v[j] = k[j], v[j], k[i], v[i] },
})
}

func Bubble(f struct {
len func() int
less func(i, j int) bool
swap func(i, j int)
}) {
length := f.len()
for i := 0; i < length; i++ {
for j := i + 1; j < length; j++ {
if f.less(j, i) {
f.swap(i, j)
}
}
}
}
43 changes: 43 additions & 0 deletions tests/testdata/gencircle/trammel/stable/mapString.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package stable

import (
"strings"

"github.com/gopherjs/gopherjs/tests/testdata/gencircle/trammel/cmp"
"github.com/gopherjs/gopherjs/tests/testdata/gencircle/trammel/collections"
"github.com/gopherjs/gopherjs/tests/testdata/gencircle/trammel/sorts"
)

func MapString[K cmp.Ordered, V any, M ~map[K]V](m M, stringer func(K, V) string) string {
// Function parameter with type parameters arguments.
result := collections.KeysAndValues(m)
keys := result.Keys
values := result.Values

sorts.Pair(keys, values)

parts := zipper(keys, values, stringer)
return `{` + strings.Join(parts, `, `) + `}`
}

func zipper[TIn1, TIn2, TOut any, SIn1 ~[]TIn1, SIn2 ~[]TIn2, F ~func(TIn1, TIn2) TOut](s1 SIn1, s2 SIn2, merge F) []TOut {
// Overly complex type parameters including a generic function type.
min := len(s1)
if len(s2) < min {
min = len(s2)
}
result := make([]any, min)
for i := 0; i < min; i++ {
result[i] = merge(s1[i], s2[i])
}
return castSlice[any, TOut](result)
}

func castSlice[TIn, TOut any, SIn ~[]TIn, SOut []TOut](s SIn) SOut {
result := make(SOut, len(s))
for i, v := range s {
// Using a type parameter to cast the type.
result[i] = any(v).(TOut)
}
return result
}
Loading