Skip to content

Commit

Permalink
Convert function, minor improvements (#1)
Browse files Browse the repository at this point in the history
* Convert function, minor improvements

* fix readme

* Should probably just retain the original license without adding all contributors
  • Loading branch information
Alexandr Stefurishin authored Dec 9, 2024
1 parent 3b85ab3 commit bd1412b
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 28 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Antonio Cheong
Copyright (c) 2024 John C. Griffin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
25 changes: 7 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,20 @@ Function signatures: https://pkg.go.dev/github.com/g-utils/overflow
```go
package overflow

type number interface {
type Int interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
}

func Add[T number](a, b T) (T, bool)
func Sub[T number](a, b T) (T, bool)
func Mul[T number](a, b T) (T, bool)
func Div[T number](a, b T) (T, bool)
func Quotient[T number](a, b T) (T, T, bool)
func Add[T Int](a, b T) (T, bool)
func Sub[T Int](a, b T) (T, bool)
func Mul[T Int](a, b T) (T, bool)
func Div[T Int](a, b T) (T, bool)
func Quotient[T Int](a, b T) (T, T, bool)
func Convert[T Int, U Int](a T) (U, bool)
```



# Copyright / License

This is a fork of https://github.com/JohnCGriffin/overflow. Generics were added to Golang in Go 1.18 (2022) and is used in place of build time code generators.

Tests for unsigned integers have also been added from github.com/rwxe/overflow in case my logic is incorrect.

```
Copyright (c) 2017 John C. Griffin,
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```
26 changes: 18 additions & 8 deletions overflow.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package overflow

type number interface {
type Int interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
}

func Add[T number](a, b T) (T, bool) {
func Add[T Int](a, b T) (T, bool) {
c := a + b
if (c > a) == (b > 0) {
return c, true
}
return c, false
}

func Sub[T number](a, b T) (T, bool) {
func Sub[T Int](a, b T) (T, bool) {
c := a - b
if (c < a) == (b > 0) {
return c, true
}
return c, false
}

func Mul[T number](a, b T) (T, bool) {
func Mul[T Int](a, b T) (T, bool) {
if a == 0 || b == 0 {
return 0, true
}
Expand All @@ -32,15 +32,25 @@ func Mul[T number](a, b T) (T, bool) {
}
return c, false
}
func Div[T number](a, b T) (T, bool) {

func Div[T Int](a, b T) (T, bool) {
q, _, ok := Quotient(a, b)
return q, ok
}
func Quotient[T number](a, b T) (T, T, bool) {

func Quotient[T Int](a, b T) (T, T, bool) {
if b == 0 {
return 0, 0, false
}
c := a / b
status := (c < 0) == ((a < 0) != (b < 0))
return c, a % b, status
ok := (c < 0) == ((a < 0) != (b < 0))
return c, a % b, ok
}

func Convert[T Int, U Int](a T) (U, bool) {
b := U(a)
if T(b) == a && (a < 0) == (b < 0) {
return b, true
}
return b, false
}
152 changes: 151 additions & 1 deletion overflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package overflow_test
import (
"math"
"testing"

"github.com/g-utils/overflow"
)
import "github.com/g-utils/overflow"

// sample all possibilities of 8 bit numbers
// by checking against 64 bit numbers
Expand Down Expand Up @@ -287,3 +288,152 @@ func TestAlgorithmsUintImpl2(t *testing.T) {
}

}

func TestConvert(t *testing.T) {
// int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

t.Run("positive: uint64->unsigned", func(t *testing.T) {
t.Parallel()
for i := uint64(0); i <= math.MaxUint8; i++ {
testConvert4[uint64, uint64, uint32, uint16, uint8](t, i, true)
}
testConvert3[uint64, uint64, uint32, uint16](t, math.MaxUint16-1, true)
testConvert3[uint64, uint64, uint32, uint16](t, math.MaxUint16, true)
testConvert2[uint64, uint64, uint32](t, math.MaxUint32-1, true)
testConvert2[uint64, uint64, uint32](t, math.MaxUint32, true)
testConvert1[uint64, uint64](t, math.MaxUint64-1, true)
testConvert1[uint64, uint64](t, math.MaxUint64, true)
})

t.Run("negative: uint64->unsigned", func(t *testing.T) {
for i := uint64(math.MaxUint32 + 1); i <= uint64(math.MaxUint32+math.MaxUint8+1); i++ {
testConvert3[uint64, uint32, uint16, uint8](t, i, false)
}
testConvert2[uint64, uint16, uint8](t, math.MaxUint16+1, false)
testConvert1[uint64, uint8](t, math.MaxUint8+1, false)
})

t.Run("positive: uint64->signed", func(t *testing.T) {
for i := uint64(0); i <= math.MaxInt8; i++ {
testConvert4[uint64, int64, int32, int16, int8](t, i, true)
}
testConvert3[uint64, int64, int32, int16](t, math.MaxInt16-1, true)
testConvert3[uint64, int64, int32, int16](t, math.MaxInt16, true)
testConvert2[uint64, int64, int32](t, math.MaxInt32-1, true)
testConvert2[uint64, int64, int32](t, math.MaxInt32, true)
testConvert1[uint64, int64](t, math.MaxInt64-1, true)
testConvert1[uint64, int64](t, math.MaxInt64, true)
})

t.Run("negative: uint64->signed", func(t *testing.T) {
for i := uint64(math.MaxInt64 + 1); i <= uint64(math.MaxInt64+math.MaxUint8+1); i++ {
testConvert4[uint64, int64, int32, int16, int8](t, i, false)
}
testConvert4[uint64, int64, int32, int16, int8](t, math.MaxUint64, false)
testConvert4[uint64, int64, int32, int16, int8](t, math.MaxUint64-1, false)
testConvert3[uint64, int32, int16, int8](t, math.MaxInt32+1, false)
testConvert2[uint64, int16, int8](t, math.MaxInt16+1, false)
testConvert1[uint64, int8](t, math.MaxInt8+1, false)
})

t.Run("positive: int64->signed", func(t *testing.T) {
for i := int64(math.MinInt8); i <= math.MaxInt8; i++ {
testConvert4[int64, int64, int32, int16, int8](t, i, true)
}
testConvert3[int64, int64, int32, int16](t, math.MinInt16, true)
testConvert3[int64, int64, int32, int16](t, math.MinInt16+1, true)
testConvert3[int64, int64, int32, int16](t, math.MaxInt16-1, true)
testConvert3[int64, int64, int32, int16](t, math.MaxInt16, true)
testConvert2[int64, int64, int32](t, math.MinInt32, true)
testConvert2[int64, int64, int32](t, math.MinInt32+1, true)
testConvert2[int64, int64, int32](t, math.MaxInt32-1, true)
testConvert2[int64, int64, int32](t, math.MaxInt32, true)
testConvert1[int64, int64](t, math.MinInt64, true)
testConvert1[int64, int64](t, math.MinInt64+1, true)
testConvert1[int64, int64](t, math.MaxInt64-1, true)
testConvert1[int64, int64](t, math.MaxInt64, true)
})

t.Run("negative: int64->signed", func(t *testing.T) {
for i := int64(math.MaxInt32 + 1); i <= int64(math.MaxInt32+math.MaxInt8+1); i++ {
testConvert3[int64, int32, int16, int8](t, i, false)
}
testConvert2[int64, int16, int8](t, math.MaxInt16+1, false)
testConvert1[int64, int8](t, math.MaxInt8+1, false)
})

t.Run("positive: int64->unsigned", func(t *testing.T) {
for i := int64(0); i <= math.MaxUint8; i++ {
testConvert4[int64, uint64, uint32, uint16, uint8](t, i, true)
}
testConvert3[int64, uint64, uint32, uint16](t, math.MaxUint16-1, true)
testConvert3[int64, uint64, uint32, uint16](t, math.MaxUint16, true)
testConvert2[int64, uint64, uint32](t, math.MaxUint32-1, true)
testConvert2[int64, uint64, uint32](t, math.MaxUint32, true)
testConvert1[int64, uint64](t, math.MaxInt64-1, true)
testConvert1[int64, uint64](t, math.MaxInt64, true)
})

t.Run("negative: int64->unsigned", func(t *testing.T) {
testConvert4[int64, uint64, uint32, uint16, uint8](t, math.MinInt64, false)
testConvert4[int64, uint64, uint32, uint16, uint8](t, math.MinInt64+1, false)
testConvert4[int64, uint64, uint32, uint16, uint8](t, -1, false)
testConvert3[int64, uint32, uint16, uint8](t, math.MaxUint32+1, false)
testConvert2[int64, uint16, int8](t, math.MaxUint16+1, false)
for i := int64(math.MaxUint8 + 1); i <= math.MaxUint8+math.MaxUint8+2; i++ {
testConvert1[int64, uint8](t, i, false)
}
})

t.Run("negative: int32|16|8->uint64", func(t *testing.T) {
testConvert1[int32, uint64](t, -1, false)
testConvert1[int32, uint64](t, math.MinInt32, false)
testConvert1[int16, uint64](t, -1, false)
testConvert1[int16, uint64](t, math.MinInt16, false)
testConvert1[int8, uint64](t, -1, false)
testConvert1[int8, uint64](t, math.MinInt8, false)
})

// already covered by reverse conversion checks:
// - "positive [u]int32|16|8 -> [u]int64"

// impossible cases:
// "negative uint32|16|8 -> [u]int64"
// "negative [u]int32|16|8 -> int64"
}

func testConvert4[T overflow.Int, U1 overflow.Int, U2 overflow.Int, U3 overflow.Int, U4 overflow.Int](t *testing.T, a T, expectOk bool) {
t.Helper()
testConvert2[T, U1, U2](t, a, expectOk)
testConvert2[T, U3, U4](t, a, expectOk)
}

func testConvert3[T overflow.Int, U1 overflow.Int, U2 overflow.Int, U3 overflow.Int](t *testing.T, a T, expectOk bool) {
t.Helper()
testConvert1[T, U1](t, a, expectOk)
testConvert1[T, U2](t, a, expectOk)
testConvert1[T, U3](t, a, expectOk)
}

func testConvert2[T overflow.Int, U1 overflow.Int, U2 overflow.Int](t *testing.T, a T, expectOk bool) {
t.Helper()
testConvert1[T, U1](t, a, expectOk)
testConvert1[T, U2](t, a, expectOk)
}

func testConvert1[T overflow.Int, U overflow.Int](t *testing.T, a T, expectOk bool) {
t.Helper()
if b, ok := overflow.Convert[T, U](a); expectOk && !ok {
t.Error("expected ok, got overflow: ", b)
} else if !expectOk && ok {
t.Error("expected overflow, got ok: ", b)
} else if a2, ok2 := overflow.Convert[U, T](b); ok && !ok2 {
t.Error("expected ok, got ok, but reverse conversion overflowed: ", a2)
}
}

func BenchmarkConvert(b *testing.B) {
for i := int64(-1_000_000_000); i < 1_000_000_000; i++ {
_, _ = overflow.Convert[int64, uint64](i)
}
}

0 comments on commit bd1412b

Please sign in to comment.