diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-02-07 11:07:39 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-02-07 11:07:39 -0500 |
commit | f2cef211cda0d86ccd42e8aa411f9865a2a22cce (patch) | |
tree | de9537c79853f496141e737206e65e33fb1637c0 /cvss/score_test.go | |
parent | 19bcc5e8df039f5de0f46fd6aa71e425a5e797bb (diff) | |
download | cvez-f2cef211cda0d86ccd42e8aa411f9865a2a22cce.tar.bz2 cvez-f2cef211cda0d86ccd42e8aa411f9865a2a22cce.zip |
cvss: rename v2Score to Score, update deps and tests
Diffstat (limited to 'cvss/score_test.go')
-rw-r--r-- | cvss/score_test.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/cvss/score_test.go b/cvss/score_test.go new file mode 100644 index 0000000..d35c4c8 --- /dev/null +++ b/cvss/score_test.go @@ -0,0 +1,92 @@ +package cvss + +import ( + "strconv" + "testing" +) + +func TestNewScore(t *testing.T) { + // pass tests + for exp := 0; exp < 100; exp++ { + val := float64(exp) / 10.0 + + t.Run(strconv.FormatInt(int64(exp), 10), func(t *testing.T) { + got, err := NewScore(val) + if err != nil { + t.Error(err) + } else if int(got) != exp { + t.Errorf("got %d, exp %d", int(got), exp) + } + }) + } + + // fail tests + failTests := []float64 { -10.0, -0.1, 10.1, 100.0 } + for _, val := range(failTests) { + t.Run(strconv.FormatFloat(val, 'f', 2, 64), func(t *testing.T) { + if got, err := NewScore(val); err == nil { + t.Errorf("got %v, exp error", got) + } + }) + } +} + +func TestScoreString(t *testing.T) { + tests := []struct { + val float64 + exp string + } { + { 0.0, "0.0" }, + { 1.0, "1.0" }, + { 1.1, "1.1" }, + { 1.2, "1.2" }, + { 2.0, "2.0" }, + { 7.5, "7.5" }, + { 10.0, "10.0" }, + } + + for _, test := range(tests) { + t.Run(test.exp, func(t *testing.T) { + if val, err := NewScore(test.val); err != nil { + t.Error(err) + } else if val.String() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", val.String(), test.exp) + } + }) + } +} + +func TestScoreFloat(t *testing.T) { + tests := []struct { + val float64 + exp float32 + } { + { 0.0, float32(0.0) }, + { 1.0, float32(1.0) }, + { 1.1, float32(1.1) }, + { 1.2, float32(1.2) }, + { 2.0, float32(2.0) }, + { 7.5, float32(7.5) }, + { 7.5, float32(7.5) }, + { 10.0, float32(10.0) }, + + // test weird cases + { 7.59, float32(7.5) }, + { 8.11111111, float32(8.1) }, + } + + for _, test := range(tests) { + t.Run(strconv.FormatFloat(test.val, 'f', 2, 64), func(t *testing.T) { + s, err := NewScore(test.val) + if err != nil { + t.Error(err) + return + } + + got := s.Float() + if got != test.exp { + t.Errorf("got \"%f\", exp \"%f\"", got, test.exp) + } + }) + } +} |