Skip to content

Commit

Permalink
Add more examples and benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
asaskevich committed Aug 19, 2020
1 parent 50839af commit 7622f00
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 48 deletions.
43 changes: 35 additions & 8 deletions converter_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
package govalidator

import "testing"
import (
"testing"
)

func BenchmarkToBoolean(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_, _ = ToBoolean("True")
_, _ = ToBoolean("False")
_, _ = ToBoolean("")
_, _ = ToBoolean("False ")
}
}

//ToString
//ToJSON
//ToFloat
//ToInt
func BenchmarkToInt(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_, _ = ToInt("-22342342.2342")
}
}

func BenchmarkToFloat(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_, _ = ToFloat("-22342342.2342")
}
}

func BenchmarkToString(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
ToString(randomArray(1000000))
}
}

func BenchmarkToJson(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_, _ = ToJSON(randomArray(1000000))
}
}
28 changes: 24 additions & 4 deletions converter_example_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package govalidator

import "time"

func ExampleToBoolean() {
// Returns the boolean value represented by the string.
// It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
Expand All @@ -9,7 +11,25 @@ func ExampleToBoolean() {
_, _ = ToBoolean("123123") // false, error
}

//ToString
//ToJSON
//ToFloat
//ToInt
func ExampleToInt() {
_, _ = ToInt(1.0) // 1, nil
_, _ = ToInt("-124") // -124, nil
_, _ = ToInt("false") // 0, error
}

func ExampleToFloat() {
_, _ = ToFloat("-124.2e123") // -124.2e123, nil
_, _ = ToFloat("false") // 0, error
}

func ExampleToString() {
_ = ToString(new(interface{})) // 0xc000090200
_ = ToString(time.Second + time.Hour) // 1h1s
_ = ToString(123) // 123
}

func ExampleToJSON() {
_, _ = ToJSON([]int{1, 2, 3}) // [1, 2, 3]
_, _ = ToJSON(map[int]int{1: 2, 2: 3}) // { "1": 2, "2": 3 }
_, _ = ToJSON(func() {}) // error
}
21 changes: 10 additions & 11 deletions numerics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package govalidator

import (
"math"
"reflect"
)

// Abs returns absolute value of number
Expand Down Expand Up @@ -68,20 +67,20 @@ func InRangeFloat64(value, left, right float64) bool {
return value >= left && value <= right
}

// InRange returns true if value lies between left and right border, generic type to handle int, float32 or float64, all types must the same type
// InRange returns true if value lies between left and right border, generic type to handle int, float32, float64 and string.
// All types must the same type.
// False if value doesn't lie in range or if it incompatible or not comparable
func InRange(value interface{}, left interface{}, right interface{}) bool {

reflectValue := reflect.TypeOf(value).Kind()
reflectLeft := reflect.TypeOf(left).Kind()
reflectRight := reflect.TypeOf(right).Kind()

if reflectValue == reflect.Int && reflectLeft == reflect.Int && reflectRight == reflect.Int {
switch value.(type) {
case int:
return InRangeInt(value.(int), left.(int), right.(int))
} else if reflectValue == reflect.Float32 && reflectLeft == reflect.Float32 && reflectRight == reflect.Float32 {
case float32:
return InRangeFloat32(value.(float32), left.(float32), right.(float32))
} else if reflectValue == reflect.Float64 && reflectLeft == reflect.Float64 && reflectRight == reflect.Float64 {
case float64:
return InRangeFloat64(value.(float64), left.(float64), right.(float64))
} else {
case string:
return value.(string) >= left.(string) && value.(string) <= right.(string)
default:
return false
}
}
Expand Down
99 changes: 99 additions & 0 deletions numerics_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package govalidator

import "testing"

func BenchmarkAbs(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = Abs(-123.3e1)
}
}

func BenchmarkSign(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = Sign(-123.3e1)
}
}

func BenchmarkIsNegative(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = IsNegative(-123.3e1)
}
}

func BenchmarkIsPositive(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = IsPositive(-123.3e1)
}
}

func BenchmarkIsNonNegative(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = IsNonNegative(-123.3e1)
}
}

func BenchmarkIsNonPositive(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = IsNonPositive(-123.3e1)
}
}

func BenchmarkInRangeInt(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = InRangeInt(10, -100, 100)
}
}

func BenchmarkInRangeFloat32(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = InRangeFloat32(10, -100, 100)
}
}

func BenchmarkInRangeFloat64(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = InRangeFloat64(10, -100, 100)
}
}

func BenchmarkInRange(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = InRange("abc", "a", "cba")
}
}

func BenchmarkIsWhole(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = IsWhole(123.132)
}
}

func BenchmarkIsNatural(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = IsNatural(123.132)
}
}
73 changes: 73 additions & 0 deletions numerics_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package govalidator

func ExampleAbs() {
_ = Abs(-123.3e1) // 123.3e1
_ = Abs(+0) // 0
_ = Abs(321) // 321
}

func ExampleSign() {
_ = Sign(-123) // -1
_ = Sign(123) // 1
_ = Sign(0) // 0
}

func ExampleIsNegative() {
_ = IsNegative(-123) // true
_ = IsNegative(0) // false
_ = IsNegative(123) // false
}

func ExampleIsPositive() {
_ = IsPositive(-123) // false
_ = IsPositive(0) // false
_ = IsPositive(123) // true
}

func ExampleIsNonNegative() {
_ = IsNonNegative(-123) // false
_ = IsNonNegative(0) // true
_ = IsNonNegative(123) // true
}

func ExampleIsNonPositive() {
_ = IsNonPositive(-123) // true
_ = IsNonPositive(0) // true
_ = IsNonPositive(123) // false
}

func ExampleInRangeInt() {
_ = InRangeInt(10, -10, 10) // true
_ = InRangeInt(10, 10, 20) // true
_ = InRangeInt(10, 11, 20) // false
}

func ExampleInRangeFloat32() {
_ = InRangeFloat32(10.02, -10.124, 10.234) // true
_ = InRangeFloat32(10.123, 10.123, 20.431) // true
_ = InRangeFloat32(10.235, 11.124, 20.235) // false
}

func ExampleInRangeFloat64() {
_ = InRangeFloat64(10.02, -10.124, 10.234) // true
_ = InRangeFloat64(10.123, 10.123, 20.431) // true
_ = InRangeFloat64(10.235, 11.124, 20.235) // false
}

func ExampleInRange() {
_ = InRange(10, 11, 20) // false
_ = InRange(10.02, -10.124, 10.234) // true
_ = InRange("abc", "a", "cba") // true
}

func ExampleIsWhole() {
_ = IsWhole(1.123) // false
_ = IsWhole(1.0) // true
_ = IsWhole(10) // true
}

func ExampleIsNatural() {
_ = IsNatural(1.123) // false
_ = IsNatural(1.0) // true
_ = IsNatural(-10) // false
}
23 changes: 23 additions & 0 deletions utils_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package govalidator

func ExampleTrim() {
// Remove from left and right spaces and "\r", "\n", "\t" characters
println(Trim(" \r\r\ntext\r \t\n", "") == "text")
// Remove from left and right characters that are between "1" and "8".
// "1-8" is like full list "12345678".
println(Trim("1234567890987654321", "1-8") == "909")
}

func ExampleWhiteList() {
// Remove all characters from string ignoring characters between "a" and "z"
println(WhiteList("a3a43a5a4a3a2a23a4a5a4a3a4", "a-z") == "aaaaaaaaaaaa")
}

func ExampleReplacePattern() {
// Replace in "http123123ftp://git534543hub.comio" following (pattern "(ftp|io|[0-9]+)"):
// - Sequence "ftp".
// - Sequence "io".
// - Sequence of digits.
// with empty string.
println(ReplacePattern("http123123ftp://git534543hub.comio", "(ftp|io|[0-9]+)", "") == "http://github.com")
}
25 changes: 0 additions & 25 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ func TestTrim(t *testing.T) {
}
}

// This small example illustrate how to work with Trim function.
func ExampleTrim() {
// Remove from left and right spaces and "\r", "\n", "\t" characters
println(Trim(" \r\r\ntext\r \t\n", "") == "text")
// Remove from left and right characters that are between "1" and "8".
// "1-8" is like full list "12345678".
println(Trim("1234567890987654321", "1-8") == "909")
}

func TestWhiteList(t *testing.T) {
t.Parallel()

Expand All @@ -136,12 +127,6 @@ func TestWhiteList(t *testing.T) {
}
}

// This small example illustrate how to work with WhiteList function.
func ExampleWhiteList() {
// Remove all characters from string ignoring characters between "a" and "z"
println(WhiteList("a3a43a5a4a3a2a23a4a5a4a3a4", "a-z") == "aaaaaaaaaaaa")
}

func TestBlackList(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -213,16 +198,6 @@ func TestReplacePattern(t *testing.T) {
}
}

// This small example illustrate how to work with ReplacePattern function.
func ExampleReplacePattern() {
// Replace in "http123123ftp://git534543hub.comio" following (pattern "(ftp|io|[0-9]+)"):
// - Sequence "ftp".
// - Sequence "io".
// - Sequence of digits.
// with empty string.
println(ReplacePattern("http123123ftp://git534543hub.comio", "(ftp|io|[0-9]+)", "") == "http://github.com")
}

func TestEscape(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 7622f00

Please sign in to comment.