Skip to content

Commit 8cc26fd

Browse files
committed
chore: fixup
Signed-off-by: moul <[email protected]>
1 parent 51b68f6 commit 8cc26fd

File tree

5 files changed

+187
-182
lines changed

5 files changed

+187
-182
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
go.sum linguist-generated text
44
gnovm/stdlibs/generated.go linguist-generated
55
gnovm/tests/stdlibs/generated.go linguist-generated
6+
*.gen.gno *.gen_test.gno linguist-generated
7+
*.gen.go *.gen_test.go linguist-generated
Lines changed: 2 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,3 @@
1-
package main
1+
package xmath
22

3-
//go:generate go run generate.go
4-
5-
import (
6-
"bytes"
7-
"fmt"
8-
"go/format"
9-
"log"
10-
"os"
11-
"strings"
12-
"text/template"
13-
)
14-
15-
type Type struct {
16-
Name string
17-
ZeroValue string
18-
Signed bool
19-
Float bool
20-
}
21-
22-
var types = []Type{
23-
{"Int8", "0", true, false},
24-
{"Int16", "0", true, false},
25-
{"Int32", "0", true, false},
26-
{"Int64", "0", true, false},
27-
{"Int", "0", true, false},
28-
{"Uint8", "0", false, false},
29-
{"Uint16", "0", false, false},
30-
{"Uint32", "0", false, false},
31-
{"Uint64", "0", false, false},
32-
{"Uint", "0", false, false},
33-
{"Float32", "0.0", true, true},
34-
{"Float64", "0.0", true, true},
35-
}
36-
37-
const sourceTpl = `package xmath
38-
39-
{{ range .Types }}
40-
// {{.Name}} helpers
41-
func Max{{.Name}}(a, b {{.Name | lower}}) {{.Name | lower}} {
42-
if a > b {
43-
return a
44-
}
45-
return b
46-
}
47-
48-
func Min{{.Name}}(a, b {{.Name | lower}}) {{.Name | lower}} {
49-
if a < b {
50-
return a
51-
}
52-
return b
53-
}
54-
55-
func Clamp{{.Name}}(value, min, max {{.Name | lower}}) {{.Name | lower}} {
56-
if value < min {
57-
return min
58-
}
59-
if value > max {
60-
return max
61-
}
62-
return value
63-
}
64-
{{if .Signed}}
65-
func Abs{{.Name}}(x {{.Name | lower}}) {{.Name | lower}} {
66-
if x < 0 {
67-
return -x
68-
}
69-
return x
70-
}
71-
72-
func Sign{{.Name}}(x {{.Name | lower}}) {{.Name | lower}} {
73-
if x < 0 {
74-
return -1
75-
}
76-
if x > 0 {
77-
return 1
78-
}
79-
return 0
80-
}
81-
{{end}}
82-
{{end}}
83-
`
84-
85-
const testTpl = `package xmath
86-
87-
import "testing"
88-
89-
{{range .Types}}
90-
func Test{{.Name}}Helpers(t *testing.T) {
91-
// Test Max{{.Name}}
92-
if Max{{.Name}}(1, 2) != 2 {
93-
t.Error("Max{{.Name}}(1, 2) should be 2")
94-
}
95-
{{if .Signed}}if Max{{.Name}}(-1, -2) != -1 {
96-
t.Error("Max{{.Name}}(-1, -2) should be -1")
97-
}{{end}}
98-
99-
// Test Min{{.Name}}
100-
if Min{{.Name}}(1, 2) != 1 {
101-
t.Error("Min{{.Name}}(1, 2) should be 1")
102-
}
103-
{{if .Signed}}if Min{{.Name}}(-1, -2) != -2 {
104-
t.Error("Min{{.Name}}(-1, -2) should be -2")
105-
}{{end}}
106-
107-
// Test Clamp{{.Name}}
108-
if Clamp{{.Name}}(5, 1, 3) != 3 {
109-
t.Error("Clamp{{.Name}}(5, 1, 3) should be 3")
110-
}
111-
if Clamp{{.Name}}(0, 1, 3) != 1 {
112-
t.Error("Clamp{{.Name}}(0, 1, 3) should be 1")
113-
}
114-
if Clamp{{.Name}}(2, 1, 3) != 2 {
115-
t.Error("Clamp{{.Name}}(2, 1, 3) should be 2")
116-
}
117-
{{if .Signed}}
118-
// Test Abs{{.Name}}
119-
if Abs{{.Name}}(-5) != 5 {
120-
t.Error("Abs{{.Name}}(-5) should be 5")
121-
}
122-
if Abs{{.Name}}(5) != 5 {
123-
t.Error("Abs{{.Name}}(5) should be 5")
124-
}
125-
126-
// Test Sign{{.Name}}
127-
if Sign{{.Name}}(-5) != -1 {
128-
t.Error("Sign{{.Name}}(-5) should be -1")
129-
}
130-
if Sign{{.Name}}(5) != 1 {
131-
t.Error("Sign{{.Name}}(5) should be 1")
132-
}
133-
if Sign{{.Name}}({{.ZeroValue}}) != 0 {
134-
t.Error("Sign{{.Name}}({{.ZeroValue}}) should be 0")
135-
}
136-
{{end}}
137-
}
138-
{{end}}
139-
`
140-
141-
func main() {
142-
funcMap := template.FuncMap{
143-
"lower": strings.ToLower,
144-
}
145-
146-
// Generate source file
147-
sourceTmpl := template.Must(template.New("source").Funcs(funcMap).Parse(sourceTpl))
148-
var sourceOut bytes.Buffer
149-
if err := sourceTmpl.Execute(&sourceOut, struct{ Types []Type }{types}); err != nil {
150-
log.Fatal(err)
151-
}
152-
153-
// Format the generated code
154-
formattedSource, err := format.Source(sourceOut.Bytes())
155-
if err != nil {
156-
log.Fatal(err)
157-
}
158-
159-
// Write source file
160-
if err := os.WriteFile("xmath.gno", formattedSource, 0644); err != nil {
161-
log.Fatal(err)
162-
}
163-
164-
// Generate test file
165-
testTmpl := template.Must(template.New("test").Parse(testTpl))
166-
var testOut bytes.Buffer
167-
if err := testTmpl.Execute(&testOut, struct{ Types []Type }{types}); err != nil {
168-
log.Fatal(err)
169-
}
170-
171-
// Format the generated test code
172-
formattedTest, err := format.Source(testOut.Bytes())
173-
if err != nil {
174-
log.Fatal(err)
175-
}
176-
177-
// Write test file
178-
if err := os.WriteFile("xmath_test.gno", formattedTest, 0644); err != nil {
179-
log.Fatal(err)
180-
}
181-
182-
fmt.Println("Generated xmath.gno and xmath_test.gno")
183-
}
3+
//go:generate go run generator.go
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//go:build ignore
2+
3+
package main
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
"go/format"
9+
"log"
10+
"os"
11+
"strings"
12+
"text/template"
13+
)
14+
15+
type Type struct {
16+
Name string
17+
ZeroValue string
18+
Signed bool
19+
Float bool
20+
}
21+
22+
var types = []Type{
23+
{"Int8", "0", true, false},
24+
{"Int16", "0", true, false},
25+
{"Int32", "0", true, false},
26+
{"Int64", "0", true, false},
27+
{"Int", "0", true, false},
28+
{"Uint8", "0", false, false},
29+
{"Uint16", "0", false, false},
30+
{"Uint32", "0", false, false},
31+
{"Uint64", "0", false, false},
32+
{"Uint", "0", false, false},
33+
{"Float32", "0.0", true, true},
34+
{"Float64", "0.0", true, true},
35+
}
36+
37+
const sourceTpl = `package xmath
38+
39+
{{ range .Types }}
40+
// {{.Name}} helpers
41+
func Max{{.Name}}(a, b {{.Name | lower}}) {{.Name | lower}} {
42+
if a > b {
43+
return a
44+
}
45+
return b
46+
}
47+
48+
func Min{{.Name}}(a, b {{.Name | lower}}) {{.Name | lower}} {
49+
if a < b {
50+
return a
51+
}
52+
return b
53+
}
54+
55+
func Clamp{{.Name}}(value, min, max {{.Name | lower}}) {{.Name | lower}} {
56+
if value < min {
57+
return min
58+
}
59+
if value > max {
60+
return max
61+
}
62+
return value
63+
}
64+
{{if .Signed}}
65+
func Abs{{.Name}}(x {{.Name | lower}}) {{.Name | lower}} {
66+
if x < 0 {
67+
return -x
68+
}
69+
return x
70+
}
71+
72+
func Sign{{.Name}}(x {{.Name | lower}}) {{.Name | lower}} {
73+
if x < 0 {
74+
return -1
75+
}
76+
if x > 0 {
77+
return 1
78+
}
79+
return 0
80+
}
81+
{{end}}
82+
{{end}}
83+
`
84+
85+
const testTpl = `package xmath
86+
87+
import "testing"
88+
89+
{{range .Types}}
90+
func Test{{.Name}}Helpers(t *testing.T) {
91+
// Test Max{{.Name}}
92+
if Max{{.Name}}(1, 2) != 2 {
93+
t.Error("Max{{.Name}}(1, 2) should be 2")
94+
}
95+
{{if .Signed}}if Max{{.Name}}(-1, -2) != -1 {
96+
t.Error("Max{{.Name}}(-1, -2) should be -1")
97+
}{{end}}
98+
99+
// Test Min{{.Name}}
100+
if Min{{.Name}}(1, 2) != 1 {
101+
t.Error("Min{{.Name}}(1, 2) should be 1")
102+
}
103+
{{if .Signed}}if Min{{.Name}}(-1, -2) != -2 {
104+
t.Error("Min{{.Name}}(-1, -2) should be -2")
105+
}{{end}}
106+
107+
// Test Clamp{{.Name}}
108+
if Clamp{{.Name}}(5, 1, 3) != 3 {
109+
t.Error("Clamp{{.Name}}(5, 1, 3) should be 3")
110+
}
111+
if Clamp{{.Name}}(0, 1, 3) != 1 {
112+
t.Error("Clamp{{.Name}}(0, 1, 3) should be 1")
113+
}
114+
if Clamp{{.Name}}(2, 1, 3) != 2 {
115+
t.Error("Clamp{{.Name}}(2, 1, 3) should be 2")
116+
}
117+
{{if .Signed}}
118+
// Test Abs{{.Name}}
119+
if Abs{{.Name}}(-5) != 5 {
120+
t.Error("Abs{{.Name}}(-5) should be 5")
121+
}
122+
if Abs{{.Name}}(5) != 5 {
123+
t.Error("Abs{{.Name}}(5) should be 5")
124+
}
125+
126+
// Test Sign{{.Name}}
127+
if Sign{{.Name}}(-5) != -1 {
128+
t.Error("Sign{{.Name}}(-5) should be -1")
129+
}
130+
if Sign{{.Name}}(5) != 1 {
131+
t.Error("Sign{{.Name}}(5) should be 1")
132+
}
133+
if Sign{{.Name}}({{.ZeroValue}}) != 0 {
134+
t.Error("Sign{{.Name}}({{.ZeroValue}}) should be 0")
135+
}
136+
{{end}}
137+
}
138+
{{end}}
139+
`
140+
141+
func main() {
142+
funcMap := template.FuncMap{
143+
"lower": strings.ToLower,
144+
}
145+
146+
// Generate source file
147+
sourceTmpl := template.Must(template.New("source").Funcs(funcMap).Parse(sourceTpl))
148+
var sourceOut bytes.Buffer
149+
if err := sourceTmpl.Execute(&sourceOut, struct{ Types []Type }{types}); err != nil {
150+
log.Fatal(err)
151+
}
152+
153+
// Format the generated code
154+
formattedSource, err := format.Source(sourceOut.Bytes())
155+
if err != nil {
156+
log.Fatal(err)
157+
}
158+
159+
// Write source file
160+
if err := os.WriteFile("xmath.gen.gno", formattedSource, 0644); err != nil {
161+
log.Fatal(err)
162+
}
163+
164+
// Generate test file
165+
testTmpl := template.Must(template.New("test").Parse(testTpl))
166+
var testOut bytes.Buffer
167+
if err := testTmpl.Execute(&testOut, struct{ Types []Type }{types}); err != nil {
168+
log.Fatal(err)
169+
}
170+
171+
// Format the generated test code
172+
formattedTest, err := format.Source(testOut.Bytes())
173+
if err != nil {
174+
log.Fatal(err)
175+
}
176+
177+
// Write test file
178+
if err := os.WriteFile("xmath.gen_test.gno", formattedTest, 0644); err != nil {
179+
log.Fatal(err)
180+
}
181+
182+
fmt.Println("Generated xmath.gen.gno and xmath.gen_test.gno")
183+
}

0 commit comments

Comments
 (0)