Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Nemere committed Nov 1, 2024
1 parent e5fdc0a commit 9ffaabd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
4 changes: 3 additions & 1 deletion api/dataimport/sdfToRSI/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func findToken(line string, prefix string, endDelim string) (string, bool) {

// Split off the first token (space separated), returns token, rest-of-line, error if any
func takeToken(line string, sep string) (string, string, bool) {
line = strings.TrimLeft(line, sep)
pos := strings.Index(line, sep)
if pos > 0 {
return line[0:pos], strings.TrimLeft(line[pos+len(sep):], " "), true
Expand All @@ -102,7 +103,8 @@ func readFloat(line string) (float32, string, error) {

f, err := strconv.ParseFloat(fStr, 32)
if err != nil {
return 0, remainder, fmt.Errorf("Failed to parse float: %v. Error: %v", fStr, err)
// Prints: Error: strconv.ParseFloat: parsing "2.L3": invalid syntax
return 0, remainder, fmt.Errorf("Error: %v", err)
}

return float32(f), remainder, nil
Expand Down
34 changes: 23 additions & 11 deletions api/dataimport/sdfToRSI/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,41 @@ func Example_readFloat() {
f, r, e := readFloat("-0.12470774 0.15369324 0.24655464 ")
fmt.Printf("%v|%v|%v\n", f, e, strings.TrimRight(r, " "))

f, r, e = readFloat(" -0.12470774 0.15369324 0.24655464 ")
f, r, e = readFloat("2.L3 0.15369324 0.24655464 ")
fmt.Printf("%v|%v|%v\n", f, e, r)

// Output:
// -0.12470774|<nil>|0.15369324 0.24655464
// 0|Failed to read token| -0.12470774 0.15369324 0.24655464
// 0|Error: strconv.ParseFloat: parsing "2.L3": invalid syntax|0.15369324 0.24655464
}

func Example_takeToken() {
tok, l, err := takeToken("gv - 0x00dd40 : 00000000 00000000 00000000 00000000 :: 0 0 0 0 ", " -")
fmt.Printf("%v|%v|%v\n", tok, l, err)
tok, l, err = takeToken("gv - 0x00dd40 : 00000000 00000000 00000000 00000000 :: 0 0 0 0 ", " ")
fmt.Printf("%v|%v|%v\n", tok, l, err)
tok, l, ok := takeToken("gv - 0x00dd40 : 00000000 00000000 00000000 00000000 :: 0 0 0 0 ", " -")
fmt.Printf("%v|%v|%v\n", tok, l, ok)
tok, l, ok = takeToken("gv - 0x00dd40 : 00000000 00000000 00000000 00000000 :: 0 0 0 0 ", " ")
fmt.Printf("%v|%v|%v\n", tok, l, ok)

tok, l, err = takeToken(" 0x00dd40 : 00000000 00000000 00000000 00000000", " ")
fmt.Printf("%v|%v|%v\n", tok, l, err)
tok, l, ok = takeToken(" 0x00dd40 : 00000000 00000000 00000000 00000000", " ")
fmt.Printf("%v|%v|%v\n", tok, l, ok)

tok, l, err = takeToken(strings.TrimLeft(" 0x00dd40 : 00000000 00000000 00000000 00000000", " "), " ")
fmt.Printf("%v|%v|%v\n", tok, l, err)
tok, l, ok = takeToken(strings.TrimLeft(" 0x00dd40 : 00000000 00000000 00000000 00000000", " "), " ")
fmt.Printf("%v|%v|%v\n", tok, l, ok)

tok, l, ok = takeToken("abc", "b")
fmt.Printf("%v|%v|%v\n", tok, l, ok)

tok, l, ok = takeToken("abc", "a")
fmt.Printf("%v|%v|%v\n", tok, l, ok)

tok, l, ok = takeToken("a", "a")
fmt.Printf("%v|%v|%v\n", tok, l, ok)

// Output:
// gv|0x00dd40 : 00000000 00000000 00000000 00000000 :: 0 0 0 0 |true
// gv|- 0x00dd40 : 00000000 00000000 00000000 00000000 :: 0 0 0 0 |true
// | 0x00dd40 : 00000000 00000000 00000000 00000000|false
// 0x00dd40|: 00000000 00000000 00000000 00000000|true
// 0x00dd40|: 00000000 00000000 00000000 00000000|true
// a|c|true
// bc||true
// ||false
}

0 comments on commit 9ffaabd

Please sign in to comment.