package tests

import (
	"os"
	"testing"
	
	{{if or .SupportsNTT .SupportsPoly -}}
	"github.com/ingonyama-zk/icicle/v2/wrappers/golang/core"
	{{.Field}} "github.com/ingonyama-zk/icicle/v2/wrappers/golang/{{.BaseImportPath}}"{{end}}
	{{if .SupportsNTT -}}
	ntt "github.com/ingonyama-zk/icicle/v2/wrappers/golang/{{.BaseImportPath}}/ntt"{{end}}
	{{if .SupportsPoly -}}
	poly "github.com/ingonyama-zk/icicle/v2/wrappers/golang/{{.BaseImportPath}}/polynomial"{{end}}

	{{if ne .GnarkImport "" -}}
	"github.com/consensys/gnark-crypto/ecc/{{.GnarkImport}}/fr/fft"
	{{end -}}
)

const (
	largestTestSize = 20
)
{{if or .SupportsNTT .SupportsPoly -}}
func initDomain[T any](largestTestSize int, cfg core.NTTConfig[T]) core.IcicleError {
	{{if ne .GnarkImport "" -}}
	rouMont, _ := fft.Generator(uint64(1 << largestTestSize))
	rou := rouMont.Bits()
	rouIcicle := {{.Field}}.{{.FieldPrefix}}Field{}
	limbs := core.ConvertUint64ArrToUint32Arr(rou[:])

	rouIcicle.FromLimbs(limbs)
	{{else -}}
	rouIcicle := {{.Field}}.{{.FieldPrefix}}Field{}
	rouIcicle.FromUint32({{.ROU}})
	{{end -}}
	e := ntt.InitDomain(rouIcicle, cfg.Ctx, false)
	return e
}{{end}}

func TestMain(m *testing.M) {
	{{if .SupportsPoly -}}poly.InitPolyBackend(){{end}}

	{{if or .SupportsNTT .SupportsPoly -}}// setup domain
	cfg := ntt.GetDefaultNttConfig()
	e := initDomain(largestTestSize, cfg)
	if e.IcicleErrorCode != core.IcicleErrorCode(0) {
		panic("initDomain failed")
	}{{end}}

	// execute tests
	os.Exit(m.Run())

	{{if or .SupportsNTT .SupportsPoly -}}// release domain
	e = ntt.ReleaseDomain(cfg.Ctx)
	if e.IcicleErrorCode != core.IcicleErrorCode(0) {
		panic("ReleaseDomain failed")
	}{{end}}
}
