Commit 1d645b76 authored by santiaago's avatar santiaago
Browse files

add ReOrder and Swap tests, fix bug.

parent b3a35115
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ func Colors(r *http.Request) (colors []color.RGBA) {

	if len(colors) == 2 && Inverse(r) {
		swap(&colors)

	}

	if order := Order(r); len(order) > 0 {
@@ -87,7 +88,7 @@ func ReOrder(order []int, pColors *[]color.RGBA) {
			}
		}
		if reOrder {
			colors = tmp
			*pColors = tmp
		}
	}
}
+75 −0
Original line number Diff line number Diff line
@@ -46,6 +46,17 @@ func TestColors(t *testing.T) {
			"http://www.tg.c?theme=frogideas&numcolors=4",
			colorMap["frogideas"],
		},
		{
			"test good input with theme and order array",
			"http://www.tg.c?theme=frogideas&numcolors=4&order=3&order=2&order=1&order=0",
			[]color.RGBA{
				colorMap["frogideas"][3],
				colorMap["frogideas"][2],
				colorMap["frogideas"][1],
				colorMap["frogideas"][0],
			},
		},

		{
			"test bad theme",
			"http://www.tg.c?theme=bad",
@@ -286,3 +297,67 @@ func TestExtraColors(t *testing.T) {
		}
	}
}

func TestReOrder(t *testing.T) {
	t.Parallel()
	colors := []color.RGBA{
		{255, 255, 255, 255},
		{0, 0, 0, 255},
		{0, 0, 0, 255},
		{255, 255, 255, 255},
	}
	expected := []color.RGBA{
		{0, 0, 0, 255},
		{0, 0, 0, 255},
		{255, 255, 255, 255},
		{255, 255, 255, 255},
	}
	ReOrder([]int{1, 2, 0, 3}, &colors)
	if !areArrayOfColorsEqual(colors, expected) {
		t.Errorf("expected %v got %v", expected, colors)
	}

	// test wrong order array does not change colors array
	colors = []color.RGBA{
		{255, 255, 255, 255},
		{0, 0, 0, 255},
		{0, 0, 0, 255},
		{255, 255, 255, 255},
	}
	expected = colors
	ReOrder([]int{1, 2, 0, 4}, &colors)
	if !areArrayOfColorsEqual(colors, expected) {
		t.Errorf("expected %v got %v", expected, colors)
	}

}

func TestSwap(t *testing.T) {
	t.Parallel()
	colors := []color.RGBA{
		{255, 255, 255, 255},
		{0, 0, 0, 255},
	}
	expected := []color.RGBA{
		{0, 0, 0, 255},
		{255, 255, 255, 255},
	}
	swap(&colors)
	if !areArrayOfColorsEqual(colors, expected) {
		t.Errorf("expected %v got %v", expected, colors)
	}
}

func areArrayOfColorsEqual(a, b []color.RGBA) bool {
	if len(a) != len(b) {
		return false
	}

	for i, _ := range a {
		if a[i] != b[i] {
			return false
		}
	}

	return true
}