From 7622f00d24bf8230480549afdd9cb75d402f6bb7 Mon Sep 17 00:00:00 2001 From: Alex Saskevich Date: Wed, 19 Aug 2020 21:09:14 +0300 Subject: [PATCH] Add more examples and benchmarks --- converter_benchmark_test.go | 43 +++++++++++++--- converter_example_test.go | 28 +++++++++-- numerics.go | 21 ++++---- numerics_benchmark_test.go | 99 +++++++++++++++++++++++++++++++++++++ numerics_example_test.go | 73 +++++++++++++++++++++++++++ utils_example_test.go | 23 +++++++++ utils_test.go | 25 ---------- 7 files changed, 264 insertions(+), 48 deletions(-) create mode 100644 numerics_benchmark_test.go create mode 100644 numerics_example_test.go create mode 100644 utils_example_test.go diff --git a/converter_benchmark_test.go b/converter_benchmark_test.go index 180144b..8b959b8 100644 --- a/converter_benchmark_test.go +++ b/converter_benchmark_test.go @@ -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)) + } +} diff --git a/converter_example_test.go b/converter_example_test.go index e0b0706..7620b43 100644 --- a/converter_example_test.go +++ b/converter_example_test.go @@ -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. @@ -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 +} diff --git a/numerics.go b/numerics.go index 2f5ac45..a5773de 100644 --- a/numerics.go +++ b/numerics.go @@ -2,7 +2,6 @@ package govalidator import ( "math" - "reflect" ) // Abs returns absolute value of number @@ -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 } } diff --git a/numerics_benchmark_test.go b/numerics_benchmark_test.go new file mode 100644 index 0000000..50e772d --- /dev/null +++ b/numerics_benchmark_test.go @@ -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) + } +} diff --git a/numerics_example_test.go b/numerics_example_test.go new file mode 100644 index 0000000..fe8ff07 --- /dev/null +++ b/numerics_example_test.go @@ -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 +} diff --git a/utils_example_test.go b/utils_example_test.go new file mode 100644 index 0000000..9126e0a --- /dev/null +++ b/utils_example_test.go @@ -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") +} diff --git a/utils_test.go b/utils_test.go index 97c97a5..8b4f87e 100644 --- a/utils_test.go +++ b/utils_test.go @@ -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() @@ -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() @@ -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()