Skip to content

Commit 64f2e5a

Browse files
Sah, NandeshwarSah, Nandeshwar
authored andcommitted
Added function Even<Type> to check if int is even or not
1 parent 9cc67e9 commit 64f2e5a

File tree

5 files changed

+350
-0
lines changed

5 files changed

+350
-0
lines changed

fp/even.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package fp
2+
3+
// EvenInt Returns true if n is even
4+
func EvenInt(v int) bool {
5+
return v%2 == 0
6+
}
7+
8+
// EvenIntPtr Returns true if n is even
9+
func EvenIntPtr(v *int) bool {
10+
return *v%2 == 0
11+
}
12+
13+
// EvenInt64 Returns true if n is even
14+
func EvenInt64(v int64) bool {
15+
return v%2 == 0
16+
}
17+
18+
// EvenInt64Ptr Returns true if n is even
19+
func EvenInt64Ptr(v *int64) bool {
20+
return *v%2 == 0
21+
}
22+
23+
// EvenInt32 Returns true if n is even
24+
func EvenInt32(v int32) bool {
25+
return v%2 == 0
26+
}
27+
28+
// EvenInt32Ptr Returns true if n is even
29+
func EvenInt32Ptr(v *int32) bool {
30+
return *v%2 == 0
31+
}
32+
33+
// EvenInt16 Returns true if n is even
34+
func EvenInt16(v int16) bool {
35+
return v%2 == 0
36+
}
37+
38+
// EvenInt16Ptr Returns true if n is even
39+
func EvenInt16Ptr(v *int16) bool {
40+
return *v%2 == 0
41+
}
42+
43+
// EvenInt8 Returns true if n is even
44+
func EvenInt8(v int8) bool {
45+
return v%2 == 0
46+
}
47+
48+
// EvenInt8Ptr Returns true if n is even
49+
func EvenInt8Ptr(v *int8) bool {
50+
return *v%2 == 0
51+
}
52+
53+
// EvenUint Returns true if n is even
54+
func EvenUint(v uint) bool {
55+
return v%2 == 0
56+
}
57+
58+
// EvenUintPtr Returns true if n is even
59+
func EvenUintPtr(v *uint) bool {
60+
return *v%2 == 0
61+
}
62+
63+
// EvenUint64 Returns true if n is even
64+
func EvenUint64(v uint64) bool {
65+
return v%2 == 0
66+
}
67+
68+
// EvenUint64Ptr Returns true if n is even
69+
func EvenUint64Ptr(v *uint64) bool {
70+
return *v%2 == 0
71+
}
72+
73+
// EvenUint32 Returns true if n is even
74+
func EvenUint32(v uint32) bool {
75+
return v%2 == 0
76+
}
77+
78+
// EvenUint32Ptr Returns true if n is even
79+
func EvenUint32Ptr(v *uint32) bool {
80+
return *v%2 == 0
81+
}
82+
83+
// EvenUint16 Returns true if n is even
84+
func EvenUint16(v uint16) bool {
85+
return v%2 == 0
86+
}
87+
88+
// EvenUint16Ptr Returns true if n is even
89+
func EvenUint16Ptr(v *uint16) bool {
90+
return *v%2 == 0
91+
}
92+
93+
// EvenUint8 Returns true if n is even
94+
func EvenUint8(v uint8) bool {
95+
return v%2 == 0
96+
}
97+
98+
// EvenUint8Ptr Returns true if n is even
99+
func EvenUint8Ptr(v *uint8) bool {
100+
return *v%2 == 0
101+
}

fp/even_test.go

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package fp
2+
3+
import (
4+
_ "errors"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestEvenInt(t *testing.T) {
10+
r := EvenInt(10)
11+
if !r {
12+
t.Errorf("EvenInt failed. Expected=true, actual=false")
13+
t.Errorf(reflect.String.String())
14+
}
15+
16+
r = EvenInt(1)
17+
if r {
18+
t.Errorf("EvenInt failed. Expected=false, actual=true")
19+
}
20+
21+
var two int = 2
22+
rPtr := EvenIntPtr(&two)
23+
if !rPtr {
24+
t.Errorf("EvenIntPtr failed. Expected=true, actual=false")
25+
}
26+
}
27+
28+
func TestEvenInt64(t *testing.T) {
29+
r := EvenInt64(10)
30+
if !r {
31+
t.Errorf("EvenInt64 failed. Expected=true, actual=false")
32+
t.Errorf(reflect.String.String())
33+
}
34+
35+
r = EvenInt64(1)
36+
if r {
37+
t.Errorf("EvenInt64 failed. Expected=false, actual=true")
38+
}
39+
40+
var two int64 = 2
41+
rPtr := EvenInt64Ptr(&two)
42+
if !rPtr {
43+
t.Errorf("EvenInt64Ptr failed. Expected=true, actual=false")
44+
}
45+
}
46+
47+
func TestEvenInt32(t *testing.T) {
48+
r := EvenInt32(10)
49+
if !r {
50+
t.Errorf("EvenInt32 failed. Expected=true, actual=false")
51+
t.Errorf(reflect.String.String())
52+
}
53+
54+
r = EvenInt32(1)
55+
if r {
56+
t.Errorf("EvenInt32 failed. Expected=false, actual=true")
57+
}
58+
59+
var two int32 = 2
60+
rPtr := EvenInt32Ptr(&two)
61+
if !rPtr {
62+
t.Errorf("EvenInt32Ptr failed. Expected=true, actual=false")
63+
}
64+
}
65+
66+
func TestEvenInt16(t *testing.T) {
67+
r := EvenInt16(10)
68+
if !r {
69+
t.Errorf("EvenInt16 failed. Expected=true, actual=false")
70+
t.Errorf(reflect.String.String())
71+
}
72+
73+
r = EvenInt16(1)
74+
if r {
75+
t.Errorf("EvenInt16 failed. Expected=false, actual=true")
76+
}
77+
78+
var two int16 = 2
79+
rPtr := EvenInt16Ptr(&two)
80+
if !rPtr {
81+
t.Errorf("EvenInt16Ptr failed. Expected=true, actual=false")
82+
}
83+
}
84+
85+
func TestEvenInt8(t *testing.T) {
86+
r := EvenInt8(10)
87+
if !r {
88+
t.Errorf("EvenInt8 failed. Expected=true, actual=false")
89+
t.Errorf(reflect.String.String())
90+
}
91+
92+
r = EvenInt8(1)
93+
if r {
94+
t.Errorf("EvenInt8 failed. Expected=false, actual=true")
95+
}
96+
97+
var two int8 = 2
98+
rPtr := EvenInt8Ptr(&two)
99+
if !rPtr {
100+
t.Errorf("EvenInt8Ptr failed. Expected=true, actual=false")
101+
}
102+
}
103+
104+
func TestEvenUint(t *testing.T) {
105+
r := EvenUint(10)
106+
if !r {
107+
t.Errorf("EvenUint failed. Expected=true, actual=false")
108+
t.Errorf(reflect.String.String())
109+
}
110+
111+
r = EvenUint(1)
112+
if r {
113+
t.Errorf("EvenUint failed. Expected=false, actual=true")
114+
}
115+
116+
var two uint = 2
117+
rPtr := EvenUintPtr(&two)
118+
if !rPtr {
119+
t.Errorf("EvenUintPtr failed. Expected=true, actual=false")
120+
}
121+
}
122+
123+
func TestEvenUint64(t *testing.T) {
124+
r := EvenUint64(10)
125+
if !r {
126+
t.Errorf("EvenUint64 failed. Expected=true, actual=false")
127+
t.Errorf(reflect.String.String())
128+
}
129+
130+
r = EvenUint64(1)
131+
if r {
132+
t.Errorf("EvenUint64 failed. Expected=false, actual=true")
133+
}
134+
135+
var two uint64 = 2
136+
rPtr := EvenUint64Ptr(&two)
137+
if !rPtr {
138+
t.Errorf("EvenUint64Ptr failed. Expected=true, actual=false")
139+
}
140+
}
141+
142+
func TestEvenUint32(t *testing.T) {
143+
r := EvenUint32(10)
144+
if !r {
145+
t.Errorf("EvenUint32 failed. Expected=true, actual=false")
146+
t.Errorf(reflect.String.String())
147+
}
148+
149+
r = EvenUint32(1)
150+
if r {
151+
t.Errorf("EvenUint32 failed. Expected=false, actual=true")
152+
}
153+
154+
var two uint32 = 2
155+
rPtr := EvenUint32Ptr(&two)
156+
if !rPtr {
157+
t.Errorf("EvenUint32Ptr failed. Expected=true, actual=false")
158+
}
159+
}
160+
161+
func TestEvenUint16(t *testing.T) {
162+
r := EvenUint16(10)
163+
if !r {
164+
t.Errorf("EvenUint16 failed. Expected=true, actual=false")
165+
t.Errorf(reflect.String.String())
166+
}
167+
168+
r = EvenUint16(1)
169+
if r {
170+
t.Errorf("EvenUint16 failed. Expected=false, actual=true")
171+
}
172+
173+
var two uint16 = 2
174+
rPtr := EvenUint16Ptr(&two)
175+
if !rPtr {
176+
t.Errorf("EvenUint16Ptr failed. Expected=true, actual=false")
177+
}
178+
}
179+
180+
func TestEvenUint8(t *testing.T) {
181+
r := EvenUint8(10)
182+
if !r {
183+
t.Errorf("EvenUint8 failed. Expected=true, actual=false")
184+
t.Errorf(reflect.String.String())
185+
}
186+
187+
r = EvenUint8(1)
188+
if r {
189+
t.Errorf("EvenUint8 failed. Expected=false, actual=true")
190+
}
191+
192+
var two uint8 = 2
193+
rPtr := EvenUint8Ptr(&two)
194+
if !rPtr {
195+
t.Errorf("EvenUint8Ptr failed. Expected=true, actual=false")
196+
}
197+
}

internal/generatefp.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ var fpCodeList = []fpCode{
150150
generatedTestFileName: "neg_test.go",
151151
},
152152

153+
fpCode{
154+
function: "Even",
155+
codeTemplate: basic.Even(),
156+
dataTypes: []string{"int", "int64", "int32", "int16", "int8", "uint", "uint64", "uint32", "uint16", "uint8"},
157+
generatedFileName: "even.go",
158+
159+
testTemplate: basic.EvenTest(),
160+
//testTemplateBool: basic.SupersetBoolTest(),
161+
generatedTestFileName: "even_test.go",
162+
},
163+
153164
fpCode{
154165
function: "Take",
155166
codeTemplate: basic.Take(),

internal/template/basic/even.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package basic
2+
3+
// Even is template
4+
func Even() string {
5+
return `
6+
// Even<FTYPE> Returns true if n is even
7+
func Even<FTYPE>(v <TYPE>) bool {
8+
return v%2 == 0
9+
}
10+
11+
// Even<FTYPE>Ptr Returns true if n is even
12+
func Even<FTYPE>Ptr(v *<TYPE>) bool {
13+
return *v%2 == 0
14+
}
15+
`
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package basic
2+
3+
// EvenTest is template to generate itself for different combination of data type.
4+
func EvenTest() string {
5+
return `
6+
func TestEven<FTYPE>(t *testing.T) {
7+
r := Even<FTYPE>(10)
8+
if !r {
9+
t.Errorf("Even<FTYPE> failed. Expected=true, actual=false")
10+
t.Errorf(reflect.String.String())
11+
}
12+
13+
r = Even<FTYPE>(1)
14+
if r {
15+
t.Errorf("Even<FTYPE> failed. Expected=false, actual=true")
16+
}
17+
18+
var two <TYPE> = 2
19+
rPtr := Even<FTYPE>Ptr(&two)
20+
if !rPtr {
21+
t.Errorf("Even<FTYPE>Ptr failed. Expected=true, actual=false")
22+
}
23+
}
24+
`
25+
}

0 commit comments

Comments
 (0)