diff --git a/slownie.go b/slownie.go index 3589bb0..37746f9 100644 --- a/slownie.go +++ b/slownie.go @@ -1,12 +1,10 @@ -package main +package slownie import ( "errors" "fmt" "math" - "math/rand" "strings" - "time" ) var _smallNumbers = []string{ @@ -37,9 +35,19 @@ var _zlotyWord = []string{"złoty", "złote", "złotych"} var _groszyWord = []string{"grosz", "grosze", "groszy"} +func round(num float64) int { + return int(num + math.Copysign(0.5, num)) +} + +func toFixed(num float64, precision int) float64 { + output := math.Pow(10, float64(precision)) + return float64(round(num*output)) / output +} + func split(value float64) (int64, int32) { pre := int64(value) - post := int32(math.Mod((value*100 + 0.5), 100)) + // post := int32((value - float64(pre)) * 100) + post := int32(math.Mod((value*100)+0.5, 100)) return pre, post } @@ -116,7 +124,9 @@ func someSlownie(value int64, some []string) string { return slownie(value) + " " + some[caseWord(int(value))] } -func kwotaSlownie(input float64) (output string, err error) { +// KwotaSlownie - return value in words of zloty and groszy +func KwotaSlownie(input float64) (output string, err error) { + input = toFixed(input, 2) defer func() { if r := recover(); r != nil { err = errors.New("too big number") @@ -128,24 +138,5 @@ func kwotaSlownie(input float64) (output string, err error) { input = -input } zloty, grosze := split(input) - return minusword + someSlownie(zloty, _zlotyWord) + fmt.Sprintf(" %d/100", grosze), nil -} - -func main() { - r := rand.New(rand.NewSource(time.Now().UnixNano())) - - for i := 0; i < 10; i++ { - x := r.Float64() * 10000000000000000 - if kwota, err := kwotaSlownie(x); err == nil { - fmt.Printf("%.2f %s\n", x, kwota) - } else { - fmt.Printf("%.2f %s\n", x, err) - } - } - x := 4831179.50 - if kwota, err := kwotaSlownie(x); err == nil { - fmt.Printf("%.2f %s\n", x, kwota) - } else { - fmt.Printf("%.2f %s\n", x, err) - } + return minusword + someSlownie(zloty, _zlotyWord) + fmt.Sprintf(" %02d/100", grosze), nil } diff --git a/slownie_test.go b/slownie_test.go new file mode 100644 index 0000000..8746864 --- /dev/null +++ b/slownie_test.go @@ -0,0 +1,82 @@ +package slownie + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "math/rand" + "net/http" + "net/url" + "regexp" + "testing" + "time" +) + +type slownieResponse struct { + Error int + Data slownieResponseData `json:"data"` +} + +type slownieResponseData struct { + Word string + Header string +} + +func TestKwotaSlownie(t *testing.T) { + client := &http.Client{} + r := rand.New(rand.NewSource(time.Now().UnixNano())) + space := regexp.MustCompile(`\s+`) + + cookie1 := &http.Cookie{Name: "PHPSESSID", Value: "shplbjsjd3o4fdt9iutbb0o233", HttpOnly: false} + + for i := 0; i < 1000; i++ { + x := r.Float64() * 1000000000 + if kwota, err := KwotaSlownie(x); err == nil { + valueAsString := fmt.Sprintf("%.2f", x) + + reqBody := url.Values{"number": {valueAsString}, "currency": {"pln"}, "country": {"pl"}, "csrf": {"bb50ebc27eaee017c346e88d9cbe2b94efb4b53a"}} + reqBodyStr := reqBody.Encode() + req, err := http.NewRequest("POST", "https://slownie.pl/ajax/word/get", bytes.NewBuffer([]byte(reqBodyStr))) + req.AddCookie(cookie1) + req.Header.Set("Content-type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + if err != nil { + t.Fatal("webrequest failed") + } + + if resp.StatusCode != 200 { + t.Fatalf("codeL %d", resp.StatusCode) + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Fatal("parsing body failed") + } + + gotSlownieResponse := slownieResponse{} + err = json.Unmarshal(body, &gotSlownieResponse) + if err != nil { + t.Fatal("unmarshaling body failed", err) + return + } + + if gotSlownieResponse.Error != 0 { + t.Fatalf("got error: %d", gotSlownieResponse.Error) + } + + their := space.ReplaceAllString(gotSlownieResponse.Data.Word, " ") + if their != kwota { + t.Errorf("%.2f\n", x) + t.Errorf("Got: %s\n", gotSlownieResponse.Data.Word) + t.Errorf("Own: %s\n", kwota) + } else { + t.Logf("%.2f sucessful", x) + } + } else { + t.Errorf("%.2f %s\n", x, err) + } + } +}