Skip to content

Commit 4d914c9

Browse files
committed
visual cleaning
1 parent 9d5dc9a commit 4d914c9

File tree

2 files changed

+50
-56
lines changed

2 files changed

+50
-56
lines changed

overflow.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
// Package overflow offers overflow-checked integer arithmetic operations for int, int32, and int64.
2-
// Each of the operations returns a result,bool combination. This was prompted by the need to know
3-
// when to flow into higher precision types from the math.big library.
4-
//
5-
// For instance, assuing a 64 bit machine:
6-
//
7-
// 10 + 20 -> 30
8-
// int(math.MaxInt64) + 1 -> -9223372036854775808
9-
//
10-
// whereas
11-
//
12-
// overflow.Add(10,20) -> (30, true)
13-
// overflow.Add(math.MaxInt64,1) -> (0, false)
14-
//
15-
// Add, Sub, Mul, Div are for int. Add64, Add32, etc. are specifically sized.
16-
//
17-
// If anybody wishes an unsigned version, submit a pull request for code and new tests.
1+
/*Package overflow offers overflow-checked integer arithmetic operations
2+
for int, int32, and int64. Each of the operations returns a
3+
result,bool combination. This was prompted by the need to know when
4+
to flow into higher precision types from the math.big library.
5+
6+
For instance, assuing a 64 bit machine:
7+
8+
10 + 20 -> 30
9+
int(math.MaxInt64) + 1 -> -9223372036854775808
10+
11+
whereas
12+
13+
overflow.Add(10,20) -> (30, true)
14+
overflow.Add(math.MaxInt64,1) -> (0, false)
15+
16+
Add, Sub, Mul, Div are for int. Add64, Add32, etc. are specifically sized.
17+
18+
If anybody wishes an unsigned version, submit a pull request for code
19+
and new tests. */
1820
package overflow
1921

2022
//go:generate ./overflow_template.sh

overflow_test.go

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ func TestAlgorithms(t *testing.T) {
3030

3131
// now the verification
3232
result, ok := Add8(a8, b8)
33-
if ok {
34-
if int64(result) != r64 {
35-
t.Errorf("failed to fail on %v + %v = %v instead of %v\n", a8, b8, result, r64)
36-
errors++
37-
}
38-
} else {
39-
if int64(result) == r64 {
40-
t.Fail()
41-
errors++
42-
}
33+
if ok && int64(result) != r64 {
34+
t.Errorf("failed to fail on %v + %v = %v instead of %v\n",
35+
a8, b8, result, r64)
36+
errors++
37+
}
38+
if !ok && int64(result) == r64 {
39+
t.Fail()
40+
errors++
4341
}
4442
}
4543

@@ -49,15 +47,13 @@ func TestAlgorithms(t *testing.T) {
4947

5048
// now the verification
5149
result, ok := Sub8(a8, b8)
52-
if ok {
53-
if int64(result) != r64 {
54-
t.Errorf("failed to fail on %v - %v = %v instead of %v\n", a8, b8, result, r64)
55-
}
56-
} else {
57-
if int64(result) == r64 {
58-
t.Fail()
59-
errors++
60-
}
50+
if ok && int64(result) != r64 {
51+
t.Errorf("failed to fail on %v - %v = %v instead of %v\n",
52+
a8, b8, result, r64)
53+
}
54+
if !ok && int64(result) == r64 {
55+
t.Fail()
56+
errors++
6157
}
6258
}
6359

@@ -67,16 +63,14 @@ func TestAlgorithms(t *testing.T) {
6763

6864
// now the verification
6965
result, ok := Mul8(a8, b8)
70-
if ok {
71-
if int64(result) != r64 {
72-
t.Errorf("failed to fail on %v * %v = %v instead of %v\n", a8, b8, result, r64)
73-
errors++
74-
}
75-
} else {
76-
if int64(result) == r64 {
77-
t.Fail()
78-
errors++
79-
}
66+
if ok && int64(result) != r64 {
67+
t.Errorf("failed to fail on %v * %v = %v instead of %v\n",
68+
a8, b8, result, r64)
69+
errors++
70+
}
71+
if !ok && int64(result) == r64 {
72+
t.Fail()
73+
errors++
8074
}
8175
}
8276

@@ -86,16 +80,14 @@ func TestAlgorithms(t *testing.T) {
8680

8781
// now the verification
8882
result, _, ok := Quotient8(a8, b8)
89-
if ok {
90-
if int64(result) != r64 {
91-
t.Errorf("failed to fail on %v / %v = %v instead of %v\n", a8, b8, result, r64)
92-
errors++
93-
}
94-
} else {
95-
if result != 0 && int64(result) == r64 {
96-
t.Fail()
97-
errors++
98-
}
83+
if ok && int64(result) != r64 {
84+
t.Errorf("failed to fail on %v / %v = %v instead of %v\n",
85+
a8, b8, result, r64)
86+
errors++
87+
}
88+
if !ok && result != 0 && int64(result) == r64 {
89+
t.Fail()
90+
errors++
9991
}
10092
}
10193
}

0 commit comments

Comments
 (0)