Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions objects/numbersmoother.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,19 @@ func SmoothAngle(objraw interface{}) (interface{}, error) {
}

func smoothPos(f float64) float64 {
return roundFloat(f, 3)
r := roundFloat(f, 3)
if r == float64(0) {
// for some reason -0.000001 is being returned as "-0"
return float64(0)
}
return r
}

func smoothRot(f float64) float64 {
return math.Mod(roundFloat(f, 0)+360, 360)
r := math.Mod(roundFloat(f, 0)+360, 360)
if r == float64(0) {
// for some reason -0.000001 is being returned as "-0"
return float64(0)
}
return r
}
208 changes: 128 additions & 80 deletions objects/numbersmoother_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,145 @@ package objects

import (
"ModCreator/types"
"math"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestPosition(t *testing.T) {
j := map[string]interface{}{
"posX": 42.4101944,
"posY": 1.49994421,
"posZ": -11.3552332,
}
got := Smooth(j)
want := map[string]interface{}{
"posX": 42.41,
"posY": 1.5,
"posZ": -11.355,
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
}

func TestDegree(t *testing.T) {
j := map[string]interface{}{
"rotX": float64(90),
"rotY": 89.83327,
"rotZ": float64(-0.004),
}
got := Smooth(j)
want := map[string]interface{}{
"rotX": float64(90),
"rotY": float64(90),
"rotZ": float64(0),
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
}

func TestDegreeAbs(t *testing.T) {
j := map[string]interface{}{
"rotX": float64(370),
"rotY": -89.83327,
"rotZ": float64(-0.004),
}
got := Smooth(j)
want := map[string]interface{}{
"rotX": float64(10),
"rotY": float64(270),
"rotZ": float64(0),
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
func TestSmooth(t *testing.T) {
for _, tc := range []struct {
name string
input, want map[string]interface{}
}{
{
name: "Position",
input: map[string]interface{}{
"posX": 42.4101944,
"posY": 1.49994421,
"posZ": -11.3552332,
},
want: map[string]interface{}{
"posX": 42.41,
"posY": 1.5,
"posZ": -11.355,
},
},
{
name: "Rotation",
input: map[string]interface{}{
"rotX": float64(90),
"rotY": 89.83327,
"rotZ": float64(-0.004),
},
want: map[string]interface{}{
"rotX": float64(90),
"rotY": float64(90),
"rotZ": float64(0),
},
},
{
name: "Rotation with overflow",
input: map[string]interface{}{
"rotX": float64(370),
"rotY": -89.83327,
"rotZ": float64(-0.004),
},
want: map[string]interface{}{
"rotX": float64(10),
"rotY": float64(270),
"rotZ": float64(0),
},
},
{
name: "Color",
input: map[string]interface{}{
"r": 0.42513,
"g": 0.333333333,
"b": 0.914525304,
"a": float64(0.5),
},
want: map[string]interface{}{
"r": 0.42513,
"g": 0.33333,
"b": 0.91453,
"a": 0.5,
},
},
{
name: "Color Transparent",
input: map[string]interface{}{
"r": 1.0,
"g": 1.0,
"b": 1.0,
"a": 0,
},
want: map[string]interface{}{
"r": 1.0,
"g": 1.0,
"b": 1.0,
"a": 0,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
got := Smooth(tc.input)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
})
}
}

func TestColor(t *testing.T) {
j := map[string]interface{}{
"r": 0.42513,
"g": 0.333333333,
"b": 0.914525304,
"a": float64(0.5),
}
got := Smooth(j)
want := map[string]interface{}{
"r": 0.42513,
"g": 0.33333,
"b": 0.91453,
"a": 0.5,
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
func TestNegativeZeroPos(t *testing.T) {
input := map[string]interface{}{
"posX": float64(0),
"posY": float64(-0),
"posZ": float64(-0.00001),
}
gotraw := Smooth(input)
got, ok := gotraw.(map[string]interface{})
if !ok {
t.Fatalf("bad typing")
}
for _, k := range posRounded {
raw, ok := got[k]
if !ok {
t.Fatalf("key %s not found", k)
}
fl, ok := raw.(float64)
if !ok {
t.Errorf("key %s is not float64; is %T", k, raw)
}
if math.Signbit(fl) {
t.Errorf("%s returned negative", k)
}
}
}

func TestColorTransparent(t *testing.T) {
j := map[string]interface{}{
"r": 1.0,
"g": 1.0,
"b": 1.0,
"a": 0,
}
got := Smooth(j)
want := map[string]interface{}{
"r": 1.0,
"g": 1.0,
"b": 1.0,
"a": 0,
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
func TestNegativeZeroRot(t *testing.T) {
input := map[string]interface{}{
"rotX": float64(0),
"rotY": float64(-0),
"rotZ": float64(-0.00001),
}
gotraw := Smooth(input)
got, ok := gotraw.(map[string]interface{})
if !ok {
t.Fatalf("bad typing")
}
for _, k := range rotRounded {
raw, ok := got[k]
if !ok {
t.Fatalf("key %s not found", k)
}
fl, ok := raw.(float64)
if !ok {
t.Errorf("key %s is not float64; is %T", k, raw)
}
if math.Signbit(fl) {
t.Errorf("%s returned negative", k)
}
}
}

Expand Down