-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from Tinyblargon/memory
Overhaul: Memory
- Loading branch information
Showing
9 changed files
with
1,578 additions
and
886 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package parse | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
) | ||
|
||
func Int(input interface{}) (int, error) { | ||
switch input := input.(type) { | ||
case float64: | ||
return int(input), nil | ||
case string: | ||
mem, err := strconv.ParseFloat(input, 64) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return int(mem), nil | ||
} | ||
return 0, nil | ||
} | ||
|
||
func Uint(input interface{}) (uint, error) { | ||
tmpInt, err := Int(input) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if tmpInt < 0 { | ||
return 0, errors.New("negative value") | ||
} | ||
return uint(tmpInt), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Int(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input interface{} | ||
output int | ||
err bool | ||
}{ | ||
{name: `float64 negative`, | ||
input: float64(-1), | ||
output: -1}, | ||
{name: `float64 positive`, | ||
input: float64(1), | ||
output: 1}, | ||
{name: `string invalid`, | ||
input: "a", | ||
err: true}, | ||
{name: `string negative`, | ||
input: "-1", | ||
output: -1}, | ||
{name: `string positive`, | ||
input: "1", | ||
output: 1}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
tmpOutput, tmpErr := Int(test.input) | ||
if test.err { | ||
require.Error(t, tmpErr) | ||
} else { | ||
require.NoError(t, tmpErr) | ||
require.Equal(t, test.output, tmpOutput) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_Uint(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input interface{} | ||
output uint | ||
err bool | ||
}{ | ||
{name: `float64 negative`, | ||
input: float64(-1), | ||
err: true}, | ||
{name: `float64 positive`, | ||
input: float64(1), | ||
output: 1}, | ||
{name: `string negative`, | ||
input: "-1", | ||
err: true}, | ||
{name: `string invalid`, | ||
input: "a", | ||
err: true}, | ||
{name: `string positive`, | ||
input: "1", | ||
output: 1}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
tmpOutput, tmpErr := Uint(test.input) | ||
if test.err { | ||
require.Error(t, tmpErr) | ||
} else { | ||
require.NoError(t, tmpErr) | ||
require.Equal(t, test.output, tmpOutput) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.