Permalink
Browse files

code review: int and uint are either 32 or 64 bits

  • Loading branch information...
1 parent 1a8d051 commit 56ec08737988f599347e1c48cdff2dc614f08eae @peterGo peterGo committed Jul 17, 2016
Showing with 33 additions and 5 deletions.
  1. +7 −5 metrics.go
  2. +26 −0 metrics_amd64_test.go
View
@@ -28,14 +28,16 @@ const (
//go:generate stringer -type=MetricType
func (m MetricType) isCompatibleInt(val int) bool {
- switch {
- case val < 0:
+ switch v := int64(val); {
+ case v < math.MinInt32:
+ return m == Int64Type
+ case v < 0:
return m == Int32Type || m == Int64Type
- case val <= math.MaxInt32:
+ case v <= math.MaxInt32:
return m == Int32Type || m == Int64Type || m == Uint32Type || m == Uint64Type
- case uint32(val) <= math.MaxUint32:
+ case v <= math.MaxUint32:
return m == Int64Type || m == Uint32Type || m == Uint64Type
- case int64(val) <= math.MaxInt64:
+ case v <= math.MaxInt64:
return m == Int64Type || m == Uint64Type
default:
return false
View
@@ -0,0 +1,26 @@
+package speed
+
+import (
+ "math"
+ "testing"
+)
+
+func TestIsCompatible64(t *testing.T) {
+ cases := []struct {
+ t MetricType
+ v interface{}
+ result bool
+ }{
+ {Int32Type, math.MinInt32 - 1, false},
+ {Int32Type, math.MinInt64, false},
+ {Uint32Type, math.MaxUint32 + 1, false},
+ {Uint32Type, math.MaxInt64, false},
+ }
+
+ for _, c := range cases {
+ r := c.t.IsCompatible(c.v)
+ if r != c.result {
+ t.Errorf("%v.IsCompatible(%v(%T)) should be %v, not %v", c.t, c.v, c.v, c.result, r)
+ }
+ }
+}

0 comments on commit 56ec087

Please sign in to comment.