1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package solution_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"math/rand"
"time"
"strconv"
"regexp"
)
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
func SumOfIntegersInternalSolution(strng string) int {
var re = regexp.MustCompile(`(?m)(\d+)`)
sum := 0
for _, match := range re.FindAllString(strng, -1) {
val, _ := strconv.Atoi(match)
sum += val
}
return sum
}
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
type Case struct {
str string
result int
}
var cases = []Case{
{"12.4", 16},
{"h3ll0w0rld", 3},
{"2 + 3 = ", 5},
{"Our company made approximately 1 million in gross revenue last quarter.", 1},
{"The Great Depression lasted from 1929 to 1939.", 3868},
{"Dogs are our best friends.", 0},
{"The30quick20brown10f0x1203jumps914ov3r1349the102l4zy dog", 3635},
}
var _ = Describe("SumOfIntegers functionallity", func() {
It("should cover basic cases", func() {
for _, testCase := range cases {
Expect(SumOfIntegersInString(testCase.str)).To(Equal(testCase.result), "The sum of Integers in '%s' should be equal to %d", testCase.str, testCase.result)
}
})
It("should cover random cases", func() {
for i:=0; i<100; i++ {
testCase := Case{StringWithCharset(100, "-.ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), 0}
testCase.result = SumOfIntegersInternalSolution(testCase.str)
Expect(SumOfIntegersInString(testCase.str)).To(Equal(testCase.result), "The sum of Integers in '%s' should be equal to %d", testCase.str, testCase.result)
}
})
})
|