Skip to content

Commit

Permalink
Fix an age-old bug with FromFloat handling of negatives. (#268)
Browse files Browse the repository at this point in the history
* Fix an age-old bug with `FromFloat` handling of negatives.

 * (M) pkg/yang/types_builtin(_test)?.go
    - This change squashes a bug that has existed for a long time
      in `FromFloat`, but only shows up on arm64. I finally have an
      arm64 machine and so found the issue. It was originally reported
      in openconfig/ygot#766. The issue is
      that uint64 of a negative float64 is undefined  -- and on arm64
      on darwin returns 0, which made some test cases fail only on this
      architecture.

* Add missing comment.

* `gofmt`
  • Loading branch information
robshakir authored Jul 10, 2024
1 parent c1a566e commit 9eb4298
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/yang/types_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,12 @@ func FromFloat(f float64) Number {
for ; Frac(f) != 0.0 && fracDig <= MaxFractionDigits; fracDig++ {
f *= 10.0
}
v := uint64(f)
negative := false
if f < 0 {
negative = true
v = -v
f = -f
}
v := uint64(f)

return Number{Negative: negative, Value: v, FractionDigits: fracDig}
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/yang/types_builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,54 @@ func Rf(a, b int64, fracDig uint8) YRange {
return YRange{n1, n2}
}

func TestFromFloat(t *testing.T) {
tests := []struct {
desc string
in float64
want Number
}{{
desc: "positive - no decimals",
in: 10.0,
want: Number{
Negative: false,
Value: 10,
FractionDigits: 0,
},
}, {
desc: "positive - decimals",
in: 10.15,
want: Number{
Negative: false,
Value: 1015,
FractionDigits: 2,
},
}, {
desc: "negative - no decimals",
in: -10.0,
want: Number{
Negative: true,
Value: 10,
FractionDigits: 0,
},
}, {
desc: "negative - decimals",
in: -10.15,
want: Number{
Negative: true,
Value: 1015,
FractionDigits: 2,
},
}}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
if got := FromFloat(tt.in); !cmp.Equal(got, tt.want) {
t.Fatalf("FromFloat(%v): did not get expected value, got: %+v, want: %+v", tt.in, got, tt.want)
}
})
}
}

func TestNumberInt(t *testing.T) {
tests := []struct {
desc string
Expand Down

0 comments on commit 9eb4298

Please sign in to comment.