-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
15 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 |
---|---|---|
@@ -1,17 +1,60 @@ | ||
package bitcoin | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
// Test address | ||
const address = "1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi2" | ||
|
||
// TestValidA58 will test the method ValidA58() | ||
func TestValidA58(t *testing.T) { | ||
|
||
valid, err := ValidA58([]byte(address)) | ||
t.Parallel() | ||
|
||
// Create the list of tests | ||
var tests = []struct { | ||
input string | ||
expectedValid bool | ||
expectedError bool | ||
}{ | ||
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi2", true, false}, | ||
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi", false, false}, | ||
{"1KCEAmV", false, false}, | ||
{"", false, false}, | ||
{"0", false, true}, | ||
} | ||
|
||
// Run tests | ||
for _, test := range tests { | ||
if valid, err := ValidA58([]byte(test.input)); err != nil && !test.expectedError { | ||
t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.input, err.Error()) | ||
} else if err == nil && test.expectedError { | ||
t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.input) | ||
} else if valid && !test.expectedValid { | ||
t.Errorf("%s Failed: [%s] inputted and was valid but should NOT be valid", t.Name(), test.input) | ||
} else if !valid && test.expectedValid { | ||
t.Errorf("%s Failed: [%s] inputted and was invalid but should be valid", t.Name(), test.input) | ||
} | ||
} | ||
} | ||
|
||
// ExampleValidA58 example using ValidA58() | ||
func ExampleValidA58() { | ||
valid, err := ValidA58([]byte("1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi2")) | ||
if err != nil { | ||
fmt.Printf("error occurred: %s", err.Error()) | ||
return | ||
} else if !valid { | ||
fmt.Printf("address is not valid: %s", "1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi2") | ||
return | ||
} else { | ||
fmt.Printf("address is valid!") | ||
} | ||
// Output:address is valid! | ||
} | ||
|
||
if !valid { | ||
t.Error("Failed to validate address", err) | ||
// BenchmarkValidA58 benchmarks the method ValidA58() | ||
func BenchmarkValidA58(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_, _ = ValidA58([]byte("1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi2")) | ||
} | ||
} |