Пример #1
0
// TestLP tests a real-valued linear programming example
func TestLP(t *testing.T) {
	lp := NewLP(0, 2)
	lp.SetVerboseLevel(NEUTRAL)
	lp.SetColName(0, "x")
	lp.SetColName(1, "y")
	assert.Equal(t, "x", lp.ColName(0))
	assert.Equal(t, "y", lp.ColName(1))

	lp.AddConstraint([]float64{120.0, 210.0}, LE, 15000)
	lp.AddConstraintSparse([]Entry{Entry{Col: 0, Val: 110.0}, Entry{Col: 1, Val: 30.0}}, LE, 4000)
	lp.AddConstraintSparse([]Entry{Entry{Col: 1, Val: 1.0}, Entry{Col: 0, Val: 1.0}}, LE, 75)

	lp.SetObjFn([]float64{143, 60})
	lp.SetMaximize()

	lpString := "/* Objective function */\nmax: +143 x +60 y;\n\n/* Constraints */\n+120 x +210 y <= 15000;\n+110 x +30 y <= 4000;\n+x +y <= 75;\n"
	assert.Equal(t, lpString, lp.WriteToString())

	lp.Solve()

	delta := 0.000001
	assert.InDelta(t, 6315.625, lp.Objective(), delta)

	vars := lp.Variables()
	assert.Equal(t, len(vars), 2)
	assert.InDelta(t, 21.875, vars[0], delta)
	assert.InDelta(t, 53.125, vars[1], delta)
}
Пример #2
0
func TestDecodeLineString(t *testing.T) {
	data, _ := hex.DecodeString("02000202020808")
	geom, err := Decode(bytes.NewReader(data))

	if err != nil {
		t.Fatalf("Failed to decode point geometry: err = %s", err)
	}

	t.Logf("Geom: %+v\n", geom)

	p, ok := geom.(*LineString)

	if !ok {
		t.Fatalf("Expected LineString geometry")
	}

	assert.Equal(t, LINESTRING, p.Type())
	assert.Equal(t, XY, p.Dim())
	assert.Equal(t, 2, len(p.Coords))

	assert.InDelta(t, 1, p.Coords[0][0], 1e-6)
	assert.InDelta(t, 1, p.Coords[0][1], 1e-6)

	assert.InDelta(t, 5, p.Coords[1][0], 1e-6)
	assert.InDelta(t, 5, p.Coords[1][1], 1e-6)
}
Пример #3
0
func TestTanhKernelShouldPass1(t *testing.T) {
	k := TanhKernel(1)

	// test different dot products which
	// should be valid

	// when constant is 0, default to -1.0

	assert.InDelta(t, math.Tanh(1.0-1.0), k([]float64{
		0.0, 1.0, 1.0, 0.0,
	}, []float64{
		0.0, 1.0, 0.0, 0.0,
	}), 5e-4, "Dot product should be valid")

	assert.InDelta(t, math.Tanh(6.0-1.0), k([]float64{
		15.0, 1.0, -1.0, 0.0,
	}, []float64{
		1.0, 1.0, 10.0, 0.0,
	}), 5e-4, "Dot product should be valid")

	assert.InDelta(t, math.Tanh(-84.0-1.0), k([]float64{
		15.0, 1.0, -1.0, 0.0,
	}, []float64{
		1.0, 1.0, 100.0, 0.0,
	}), 5e-4, "Dot product should be valid")
}
Пример #4
0
// TestThreeSpecsOneOverfull verifies that the scheduler behaves reasonably
// if one work spec has more jobs than its weight suggests.
func TestThreeSpecsOneOverfull(t *testing.T) {
	metas := map[string]*WorkSpecMeta{
		"one": &WorkSpecMeta{
			Weight:         1,
			PendingCount:   0,
			AvailableCount: 1000,
		},
		"two": &WorkSpecMeta{
			Weight:         5,
			PendingCount:   0,
			AvailableCount: 1000,
		},
		"three": &WorkSpecMeta{
			Weight:         1,
			PendingCount:   99,
			AvailableCount: 1000,
		},
	}
	trials := 1000
	counts := runScheduler(t, metas, trials)
	// This setup produces a negative score for "three"!  "one"
	// should have a score of 100, and "two" 500, but "three"
	// should come up with
	// (weight * (total pending + 1)) - pending * total weight
	// 1 * 100 - 99 * 7 = 100 - 693 = -593
	// and so "three" should basically just get ignored.
	assert.InDelta(t, trials*1/6, counts["one"], 3*stdDev(trials, 1, 6))
	assert.InDelta(t, trials*5/6, counts["two"], 3*stdDev(trials, 5, 6))
}
Пример #5
0
func TestLinearRegression(t *testing.T) {
	// http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Norris.dat
	data := [][]float64{{0.1, 0.2}, {338.8, 337.4}, {118.1, 118.2},
		{888.0, 884.6}, {9.2, 10.1}, {228.1, 226.5}, {668.5, 666.3}, {998.5, 996.3},
		{449.1, 448.6}, {778.9, 777.0}, {559.2, 558.2}, {0.3, 0.4}, {0.1, 0.6}, {778.1, 775.5},
		{668.8, 666.9}, {339.3, 338.0}, {448.9, 447.5}, {10.8, 11.6}, {557.7, 556.0},
		{228.3, 228.1}, {998.0, 995.8}, {888.8, 887.6}, {119.6, 120.2}, {0.3, 0.3},
		{0.6, 0.3}, {557.6, 556.8}, {339.3, 339.1}, {888.0, 887.2}, {998.5, 999.0},
		{778.9, 779.0}, {10.2, 11.1}, {117.6, 118.3}, {228.9, 229.2}, {668.4, 669.1},
		{449.2, 448.9}, {0.2, 0.5}}

	var xArray []float64
	var yArray []float64

	for _, values := range data {
		xArray = append(xArray, values[1])
		yArray = append(yArray, values[0])
	}

	regression := NewRegression()
	regression.PushAll(xArray, yArray)
	m, c := regression.Coefficients()

	assert := assert.New(t)
	assert.InDelta(m, 1.0021168180204547, 10E-12, "slope is not equal to spec")
	assert.InDelta(c, -0.262323073774029, 10E-12, "intercept is not equal to spec within reason")
}
Пример #6
0
// TestTwoUnequalSpecsWithWork verifies that the simplified scheduler
// picks two equivalent work specs with very different weights, and
// with some pending work.
func TestTwoUnequalSpecsWithWork(t *testing.T) {
	metas := map[string]*WorkSpecMeta{
		"one": &WorkSpecMeta{
			Weight:         1,
			AvailableCount: 1000,
		},
		"two": &WorkSpecMeta{
			Weight:         10,
			PendingCount:   2,
			AvailableCount: 998,
		},
	}
	trials := 1000
	counts := runScheduler(t, metas, trials)
	// These actual ratios come from the way the scheduler makes
	// its choices.  There are 2 work units pending, and there
	// will be 3 in total if one more is added.  Work spec "one"
	// "wants" 1/11 of this total, or 3/11 in all.  Work spec
	// "two" "wants" 10/11 of this total, or 30/11, but already
	// has 22/11 pending, so it "wants" 8/11 more.
	assert.InDelta(t, trials*3/11, counts["one"],
		3*stdDev(trials, 3, 11))
	assert.InDelta(t, trials*8/11, counts["two"],
		3*stdDev(trials, 8, 11))
}
Пример #7
0
func testJobs(t *testing.T, hd *HealthD) {
	recorder := httptest.NewRecorder()
	request, _ := http.NewRequest("GET", "/healthd/jobs", nil)
	hd.apiRouter().ServeHTTP(recorder, request)
	assert.Equal(t, 200, recorder.Code)

	var resp ApiResponseJobs
	err := json.Unmarshal(recorder.Body.Bytes(), &resp)

	assert.NoError(t, err)
	assert.Equal(t, len(resp.Jobs), 1)
	job := resp.Jobs[0]
	assert.Equal(t, job.Name, "foo")
	assert.EqualValues(t, job.Count, 2)
	assert.EqualValues(t, job.CountSuccess, 1)
	assert.EqualValues(t, job.CountValidationError, 1)
	assert.EqualValues(t, job.CountError, 0)
	assert.EqualValues(t, job.CountPanic, 0)
	assert.EqualValues(t, job.CountJunk, 0)
	assert.EqualValues(t, job.NanosSum, 14443)
	assert.EqualValues(t, job.NanosMin, 5678)
	assert.EqualValues(t, job.NanosMax, 8765)
	assert.InDelta(t, job.NanosAvg, 7221.5, 0.01)
	assert.InDelta(t, job.NanosSumSquares, 1.09064909e+08, 0.01)
	assert.InDelta(t, job.NanosStdDev, 2182.8386, 0.01)
}
Пример #8
0
func TestLinesWithSimilarAngle(t *testing.T) {
	examples := []struct {
		angle   float64
		lines   []polarLine
		similar []polarLine
		other   []polarLine
	}{
		{
			angle:   0,
			lines:   []polarLine{polarLine{Theta: 0, Distance: 1}, polarLine{Theta: 0, Distance: 1000}, polarLine{Theta: 0.49}, polarLine{Theta: 0.5}, polarLine{Theta: -0.49}, polarLine{Theta: -0.5}},
			similar: []polarLine{polarLine{Theta: 0, Distance: 1}, polarLine{Theta: 0, Distance: 1000}, polarLine{Theta: 0.49}, polarLine{Theta: -0.49}},
			other:   []polarLine{polarLine{Theta: 0.5}, polarLine{Theta: -0.5}},
		},
		{
			angle:   2 * math.Pi,
			lines:   []polarLine{polarLine{Theta: 0, Distance: 1}, polarLine{Theta: 0, Distance: 1000}, polarLine{Theta: 0.49}, polarLine{Theta: 0.5}, polarLine{Theta: -0.49}, polarLine{Theta: -0.5}},
			similar: []polarLine{polarLine{Theta: 0, Distance: 1}, polarLine{Theta: 0, Distance: 1000}, polarLine{Theta: 0.49}, polarLine{Theta: -0.49}},
			other:   []polarLine{polarLine{Theta: 0.5}, polarLine{Theta: -0.5}},
		},
		{
			angle:   math.Pi,
			lines:   []polarLine{polarLine{Theta: math.Pi + 0, Distance: 1}, polarLine{Theta: math.Pi + 0, Distance: 1000}, polarLine{Theta: math.Pi + 0.49}, polarLine{Theta: math.Pi + 0.5}, polarLine{Theta: math.Pi - 0.49}, polarLine{Theta: math.Pi - 0.5}},
			similar: []polarLine{polarLine{Theta: math.Pi + 0, Distance: 1}, polarLine{Theta: math.Pi + 0, Distance: 1000}, polarLine{Theta: math.Pi + 0.49}, polarLine{Theta: math.Pi - 0.49}},
			other:   []polarLine{polarLine{Theta: math.Pi + 0.5}, polarLine{Theta: math.Pi - 0.5}},
		},
		{
			angle:   math.Pi,
			lines:   []polarLine{polarLine{Theta: 0, Distance: 1}, polarLine{Theta: 0, Distance: 1000}, polarLine{Theta: 0.49}, polarLine{Theta: 0.5}, polarLine{Theta: -0.49}, polarLine{Theta: -0.5}},
			similar: []polarLine{},
			other:   []polarLine{polarLine{Theta: 0, Distance: 1}, polarLine{Theta: 0, Distance: 1000}, polarLine{Theta: 0.49}, polarLine{Theta: 0.5}, polarLine{Theta: -0.49}, polarLine{Theta: -0.5}},
		},
		{
			angle:   0,
			lines:   []polarLine{polarLine{Theta: 0, Distance: 1}, polarLine{Theta: 0, Distance: 1000}, polarLine{Theta: 0.49}, polarLine{Theta: -0.49}},
			similar: []polarLine{polarLine{Theta: 0, Distance: 1}, polarLine{Theta: 0, Distance: 1000}, polarLine{Theta: 0.49}, polarLine{Theta: -0.49}},
			other:   []polarLine{},
		},
	}

	for _, tt := range examples {
		similar, other := linesWithSimilarAngle(tt.lines, tt.angle)
		if !assert.Len(t, similar, len(tt.similar)) {
			t.Logf("Got:\n%v\nexpecting:\n%v", similar, tt.similar)
			t.FailNow()
		}
		for i, line := range similar {
			assert.InDelta(t, tt.similar[i].Theta, line.Theta, thetaDelta)
			assert.Equal(t, tt.similar[i].Distance, line.Distance)
		}

		if !assert.Len(t, other, len(tt.other)) {
			t.Logf("Got:\n%v\nexpecting:\n%v", similar, tt.similar)
			t.FailNow()
		}
		for i, line := range other {
			assert.InDelta(t, tt.other[i].Theta, line.Theta, thetaDelta)
			assert.Equal(t, tt.other[i].Distance, line.Distance)
		}
	}
}
Пример #9
0
// TestMIP tests a mixed-integer programming example
func TestMIP(t *testing.T) {
	lp := NewLP(0, 4)
	lp.AddConstraintSparse([]Entry{{0, 1.0}, {1, 1.0}}, LE, 5.0)
	lp.AddConstraintSparse([]Entry{{0, 2.0}, {1, -1.0}}, GE, 0.0)
	lp.AddConstraintSparse([]Entry{{0, 1.0}, {1, 3.0}}, GE, 0.0)
	lp.AddConstraintSparse([]Entry{{2, 1.0}, {3, 1.0}}, GE, 0.5)
	lp.AddConstraintSparse([]Entry{{2, 1.0}}, GE, 1.1)
	lp.SetObjFn([]float64{-1.0, -2.0, 0.1, 3.0})

	lp.SetInt(2, true)
	assert.Equal(t, lp.IsInt(2), true)

	lp.Solve()

	delta := 0.000001
	assert.InDelta(t, -8.133333333, lp.Objective(), delta)

	vars := lp.Variables()
	assert.Equal(t, lp.NumCols(), 4)
	assert.Equal(t, len(vars), 4)
	assert.InDelta(t, 1.6666666666, vars[0], delta)
	assert.InDelta(t, 3.3333333333, vars[1], delta)
	assert.InDelta(t, 2.0, vars[2], delta)
	assert.InDelta(t, 0.0, vars[3], delta)
}
Пример #10
0
func TestEncodePoint(t *testing.T) {
	expected := &geom.Point{
		geom.Hdr{
			Dim:  geom.XYZ,
			Srid: 27700,
		},
		[]float64{-0.118340, 51.503475, -12.34567890},
	}

	var sb bytes.Buffer
	err := Encode(expected, &sb)

	if err != nil {
		t.Fatalf("failed to marshal point: %s", err)
	}

	got := make(map[string]interface{})
	err = json.Unmarshal(sb.Bytes(), &got)

	if err != nil {
		t.Fatalf("Failed to parse generated GeoJSON: error %s", err)
	}

	t.Logf("%s\n", string(sb.Bytes()))

	assert.Equal(t, "Point", got["type"])

	coords := got["coordinates"].([]interface{})
	assert.InDelta(t, -0.118340, coords[0].(float64), 1e-9)
	assert.InDelta(t, 51.503475, coords[1].(float64), 1e-9)
	assert.InDelta(t, -12.34567890, coords[2].(float64), 1e-9)
}
Пример #11
0
func TestPolygon(t *testing.T) {
	datasets := []struct {
		data string
		srid uint32
		dim  geom.Dimension
	}{
		{"002000000300006c340000000100000005403e0000000000004024000000000000404400000000000040440000000000004034000000000000404400000000000040240000000000004034000000000000403e0000000000004024000000000000", 27700, geom.XY}, // ewkb_xdr
		{"0103000020346c000001000000050000000000000000003e4000000000000024400000000000004440000000000000444000000000000034400000000000004440000000000000244000000000000034400000000000003e400000000000002440", 27700, geom.XY}, // ewkb_hdr
		{"00000000030000000100000005403e0000000000004024000000000000404400000000000040440000000000004034000000000000404400000000000040240000000000004034000000000000403e0000000000004024000000000000", 0, geom.XY},             // wkb_hdr
		{"010300000001000000050000000000000000003e4000000000000024400000000000004440000000000000444000000000000034400000000000004440000000000000244000000000000034400000000000003e400000000000002440", 0, geom.XY},             // wkb_xdr
	}

	for _, dataset := range datasets {
		t.Log("Decoding HEX String: DIM = ", dataset.dim, ", Data = ", dataset.data)
		data, err := hex.DecodeString(dataset.data)

		if err != nil {
			t.Fatal("Failed to decode HEX string: err = ", err)
		}

		r := bytes.NewReader(data)

		g, err := Decode(r)

		if err != nil {
			t.Fatal("Failed to parse Polygon: err = ", err)
		}

		assert.Equal(t, "polygon", g.Type())

		polygon := g.(*geom.Polygon)

		t.Log(polygon)

		assert.Equal(t, dataset.dim, polygon.Dimension(), "Expected dim %v, but got %v ", dataset.dim, g.Dimension())
		assert.Equal(t, dataset.srid, polygon.SRID(), "Expected srid %v, but got %v ", dataset.srid, g.SRID())

		assert.Equal(t, 1, len(polygon.Rings))

		lr := polygon.Rings[0]

		assert.Equal(t, 5, len(lr.Coordinates))

		assert.InDelta(t, 30, lr.Coordinates[0][0], 1e-9)
		assert.InDelta(t, 10, lr.Coordinates[0][1], 1e-9)

		assert.InDelta(t, 40, lr.Coordinates[1][0], 1e-9)
		assert.InDelta(t, 40, lr.Coordinates[1][1], 1e-9)

		assert.InDelta(t, 20, lr.Coordinates[2][0], 1e-9)
		assert.InDelta(t, 40, lr.Coordinates[2][1], 1e-9)

		assert.InDelta(t, 10, lr.Coordinates[3][0], 1e-9)
		assert.InDelta(t, 20, lr.Coordinates[3][1], 1e-9)

		assert.InDelta(t, 30, lr.Coordinates[4][0], 1e-9)
		assert.InDelta(t, 10, lr.Coordinates[4][1], 1e-9)
	}
}
Пример #12
0
func assertEqualColours(t *testing.T, expected *Colour, c *Colour) {
	assert := assert.New(t)
	colourEpsilon := 0.0001

	assert.InDelta(expected.R, c.R, colourEpsilon)
	assert.InDelta(expected.G, c.G, colourEpsilon)
	assert.InDelta(expected.B, c.B, colourEpsilon)
}
Пример #13
0
func TestEncodeMultiPoint(t *testing.T) {
	expected := &geom.MultiPoint{
		geom.Hdr{
			Dim:  geom.XY,
			Srid: 4326,
		},
		[]geom.Point{
			{
				geom.Hdr{
					Dim:  geom.XY,
					Srid: 4326,
				},
				[]float64{-105.01621, 39.57422},
			},
			{
				geom.Hdr{
					Dim:  geom.XY,
					Srid: 4326,
				},
				[]float64{-80.6665134, 35.0539943},
			},
		},
	}

	var sb bytes.Buffer
	err := Encode(expected, &sb)

	if err != nil {
		t.Fatalf("failed to marshal MultiPoint: %s", err)
	}

	got := make(map[string]interface{})
	err = json.Unmarshal(sb.Bytes(), &got)

	if err != nil {
		t.Fatalf("Failed to parse generated GeoJSON: error %s", err)
	}

	t.Logf("%s\n", string(sb.Bytes()))

	assert.Equal(t, "MultiPoint", got["type"])

	coords := got["coordinates"].([]interface{})

	assert.Equal(t, 2, len(coords))

	coord := coords[0].([]interface{})

	for idx, value := range expected.Points[0].Coordinate {
		assert.InDelta(t, value, coord[idx].(float64), 1e-9)
	}

	coord = coords[1].([]interface{})

	for idx, value := range expected.Points[1].Coordinate {
		assert.InDelta(t, value, coord[idx].(float64), 1e-9)
	}
}
Пример #14
0
func TestNormalising(t *testing.T) {
	assert := assert.New(t)
	v := NewVec3(1, 2, 3)
	normalised := v.Normalised()
	v.Normalise()

	assert.InDelta(1, v.Length(), Epsilon, "Normalising should make vector's lenght 1")
	assert.InDelta(1, normalised.Length(), Epsilon, "Normalised should return vector with length 1")
}
Пример #15
0
func TestOnlineOneDXShouldPass1(t *testing.T) {
	// create the channel of data and errors
	stream := make(chan base.Datapoint, 100)
	errors := make(chan error, 20)

	model := NewLogistic(base.StochasticGA, .0001, 0, 0, nil, nil, 1)

	go model.OnlineLearn(errors, stream, func(theta [][]float64) {})

	// start passing data to our datastream
	//
	// we could have data already in our channel
	// when we instantiated the Perceptron, though
	for iter := 0; iter < 3000; iter++ {
		for i := -40.0; i < 40; i += 0.15 {
			if 10+i/2 > 0 {
				stream <- base.Datapoint{
					X: []float64{i},
					Y: []float64{1.0},
				}
			} else {
				stream <- base.Datapoint{
					X: []float64{i},
					Y: []float64{0},
				}
			}
		}
	}

	// close the dataset
	close(stream)

	err, more := <-errors

	assert.Nil(t, err, "Learning error should be nil")
	assert.False(t, more, "There should be no errors returned")

	// test a larger dataset now
	iter := 0
	for i := -100.0; i < 100; i += 0.347 {
		guess, err := model.Predict([]float64{i})
		assert.Nil(t, err, "Prediction error should be nil")
		assert.Len(t, guess, 1, "Guess should have length 1")

		if i/2+10 > 0 {
			assert.InDelta(t, 1.0, guess[0], 0.499, "Guess should be 1 for i=%v", i)
		} else {
			assert.InDelta(t, 0.0, guess[0], 0.499, "Guess should be 0 for i=%v", i)
		}
		iter++
	}
	fmt.Printf("Iter: %v\n", iter)
}
Пример #16
0
func TestDistance(t *testing.T) {
	assert.InDelta(t, float64(distance(waterloo, kingsCross)), 3080.074, 0.001)

	d := math.Sqrt(float64(approximateSquareDistance(waterloo, kingsCross)))
	assert.InDelta(t, d, 3074.987, 0.001)

	d = math.Sqrt(float64(approximateSquareDistance(leicester, coventGarden)))
	assert.InDelta(t, d, 305.662, 0.001)

	d = math.Sqrt(float64(approximateSquareDistance(oxford, embankment)))
	assert.InDelta(t, d, 1593.763, 0.001)
}
Пример #17
0
func TestCentroidsAndTraces(t *testing.T) {
	centroids, traces, f, reverse := CentroidsAndTraces(paramset, traceparams, 2)
	assert.Equal(t, 2, len(centroids))
	assert.Equal(t, 4, len(traces))
	var tr4 *Trace
	for _, clusterable := range traces {
		tr4 = clusterable.(*Trace)
		if tr4.ID == "tr4" {
			break
		}
	}
	assert.Equal(t, 0, tr4.Params[1], "Missing param should map to 0.")
	assert.Equal(t, 3, tr4.Params[0], "The value gpu should sort to last of the three params.")
	assert.Equal(t, traceparams["tr4"], reverse(tr4))
	kmeansCentroid := f([]kmeans.Clusterable{tr4, tr4})
	centroid := kmeansCentroid.(*Centroid)
	assert.Equal(t, 0.0, centroid.Distance(tr4))
	var tr1 *Trace
	for _, clusterable := range traces {
		tr1 = clusterable.(*Trace)
		if tr1.ID == "tr1" {
			break
		}
	}
	assert.Equal(t, 2.0, centroid.Distance(tr1))

	// Now run the k-means algorithm, which is deterministic for a fixed set of
	// starting centroids, so pick our centroids explicitly so we always get the
	// same answer.
	obs := make([]kmeans.Clusterable, len(traces))
	for i, tr := range traces {
		obs[i] = tr
	}
	cent := []kmeans.Centroid{
		f([]kmeans.Clusterable{tr1}),
		f([]kmeans.Clusterable{tr4}),
	}
	kmCentroids, kmClusters := kmeans.KMeans(obs, cent, 2, 10, f)
	assert.Equal(t, 2, len(kmCentroids))
	assert.Equal(t, 2, len(kmClusters))
	assert.Equal(t, 3, len(kmClusters[0]))
	assert.Equal(t, 1, len(kmClusters[1]))
	assert.Equal(t, "tr4", kmClusters[1][0].(*Trace).ID, "tr4 should be the singe member of the second cluster.")
	assert.InDelta(t, 2.7748, kmeans.TotalError(obs, kmCentroids), 0.01)

	// Run k-means again but with just one centroid and show the total error gets
	// larger.
	kmCentroids, kmClusters = kmeans.KMeans(obs, cent[:1], 2, 10, f)
	assert.Equal(t, 1, len(kmCentroids))
	assert.Equal(t, 1, len(kmClusters))
	assert.Equal(t, 4, len(kmClusters[0]))
	assert.InDelta(t, 4.42496, kmeans.TotalError(obs, kmCentroids), 0.01)
}
Пример #18
0
// Test that ping command output is processed properly
func TestProcessPingOutput(t *testing.T) {
	trans, rec, avg, err := processPingOutput(bsdPingOutput)
	assert.NoError(t, err)
	assert.Equal(t, 5, trans, "5 packets were transmitted")
	assert.Equal(t, 5, rec, "5 packets were transmitted")
	assert.InDelta(t, 20.224, avg, 0.001)

	trans, rec, avg, err = processPingOutput(linuxPingOutput)
	assert.NoError(t, err)
	assert.Equal(t, 5, trans, "5 packets were transmitted")
	assert.Equal(t, 5, rec, "5 packets were transmitted")
	assert.InDelta(t, 43.628, avg, 0.001)
}
Пример #19
0
func TestQueueConsumerRunStopsGracefullyWhenCancelled(t *testing.T) {
	// log to /dev/null because the deleter is chatty
	log.SetOutput(ioutil.Discard)
	defer func() {
		log.SetOutput(os.Stderr)
	}()

	ctl := gomock.NewController(t)
	defer ctl.Finish()

	// delay so that the cancel occurs mid-receive
	delay := func(x interface{}) {
		time.Sleep(10 * time.Millisecond)
	}
	m := mock.NewMockSQSAPI(ctl)
	m.EXPECT().ReceiveMessage(gomock.Any()).Do(delay).Return(&sqs.ReceiveMessageOutput{}, nil).AnyTimes()
	m.EXPECT().DeleteMessageBatch(gomock.Any()).AnyTimes().Return(&sqs.DeleteMessageBatchOutput{}, nil)
	m.EXPECT().ChangeMessageVisibilityBatch(gomock.Any()).AnyTimes()

	s := &SQSService{Svc: m}
	q := NewConsumer(s, noop)
	q.delayAfterReceiveError = time.Millisecond

	ngo := runtime.NumGoroutine()

	// wait long enough to ensure ReceiveMessage is running
	ctx, _ := context.WithTimeout(context.Background(), 5*time.Millisecond)
	err := q.Run(ctx)

	assert.Error(t, err)

	time.Sleep(time.Millisecond) // time for goroutines to end
	assert.InDelta(t, ngo, runtime.NumGoroutine(), 2, "Should not leak goroutines")
}
Пример #20
0
func TestHighTrafficUDP(t *testing.T) {
	listener := UdpListener{
		ServiceAddress:         ":8126",
		AllowedPendingMessages: 100000,
	}
	listener.parser, _ = parsers.NewInfluxParser()
	acc := &testutil.Accumulator{}

	// send multiple messages to socket
	err := listener.Start(acc)
	require.NoError(t, err)

	time.Sleep(time.Millisecond * 25)
	conn, err := net.Dial("udp", "127.0.0.1:8126")
	require.NoError(t, err)
	for i := 0; i < 20000; i++ {
		// arbitrary, just to give the OS buffer some slack handling the
		// packet storm.
		time.Sleep(time.Microsecond)
		fmt.Fprintf(conn, testMsgs)
	}
	time.Sleep(time.Millisecond)
	listener.Stop()

	// this is not an exact science, since UDP packets can easily get lost or
	// dropped, but assume that the OS will be able to
	// handle at least 90% of the sent UDP packets.
	assert.InDelta(t, 100000, len(acc.Metrics), 10000)
}
Пример #21
0
func TestThatPIDOutputMatchesTheSimulation(t *testing.T) {

	P := 0.0870095459081994   // Proportional coefficient
	I := 7.32612847120554e-05 // Integrative coefficient
	D := 22.0896577752675     // Derivative coefficient
	N := 0.25625893108953     // Derivative filter coefficient

	pidController := New(
		P,
		I,
		D,
		N,
		conf.Conf.MinPIDOutputLimits,
		conf.Conf.MaxPIDOutputLimits)

	pidController.Set(0)

	for i := 0; i < len(simin); i++ {
		in := simin[i]
		expectedOut := simout[i]

		output := pidController.updateWithDuration(-in, 1.)

		assert.InDelta(t, expectedOut, output, 1e-4, "supposed to be the same")
	}

}
Пример #22
0
func TestHoughLines(t *testing.T) {
	timg := image.NewGray(image.Rect(0, 0, 700, 500))
	timg.SetGray(10, 10, color.Gray{1})
	timg.SetGray(200, 10, color.Gray{1})
	timg.SetGray(400, 10, color.Gray{1})
	timg.SetGray(10, 200, color.Gray{1})
	timg.SetGray(10, 400, color.Gray{1})

	lines := houghLines(*timg, nil, 0, 10)
	if !assert.Len(t, lines, 6) {
		t.FailNow()
	}

	expectedLines := []polarLine{
		polarLine{Theta: 1.570796, Distance: 10, Count: 3},
		polarLine{Theta: 0.000000, Distance: 10, Count: 3},
		polarLine{Theta: 0.785398, Distance: 148, Count: 2},
		polarLine{Theta: 0.453786, Distance: 184, Count: 2},
		polarLine{Theta: 1.117011, Distance: 184, Count: 2},
		polarLine{Theta: 0.785398, Distance: 290, Count: 2},
	}
	for i, line := range lines {
		expected := expectedLines[i]
		thetaOk := assert.InDelta(t, expected.Theta, line.Theta, 0.0001)
		distanceOk := assert.Equal(t, expected.Distance, line.Distance)
		countOk := assert.Equal(t, expected.Count, line.Count)
		if !thetaOk || !distanceOk || !countOk {
			t.Fatalf("%v expected to equal %v", line, expected)
		}
	}
}
Пример #23
0
// TestTwoSpecsContinuous tests that a continuous work spec can be
// returned according to its weight, provided it has no work units.
func TestTwoSpecsContinuous(t *testing.T) {
	metas := map[string]*WorkSpecMeta{
		"one": &WorkSpecMeta{
			Weight:         1,
			AvailableCount: 1000,
		},
		"two": &WorkSpecMeta{
			Weight:     1,
			Continuous: true,
		},
	}
	trials := 1000
	counts := runScheduler(t, metas, trials)
	assert.InDelta(t, trials/2, counts["one"], 3*stdDev(trials, 1, 2))
	assert.InDelta(t, trials/2, counts["two"], 3*stdDev(trials, 1, 2))
}
Пример #24
0
func TestStartStop(t *testing.T) {
	assert := assert.New(t)

	startGoroutineNum := runtime.NumGoroutine()

	for i := 0; i < 10; i++ {
		qa.Root(t, func(root string) {
			configFile := TestConfig(root)

			app := carbon.New(configFile)

			assert.NoError(app.ParseConfig())
			assert.NoError(app.Start())

			app.Stop()
		})
	}

	endGoroutineNum := runtime.NumGoroutine()

	// GC worker etc
	if !assert.InDelta(startGoroutineNum, endGoroutineNum, 3) {
		p := pprof.Lookup("goroutine")
		p.WriteTo(os.Stdout, 1)
	}

}
Пример #25
0
func TestVirtual_memory(t *testing.T) {
	v, err := VirtualMemory()
	if err != nil {
		t.Errorf("error %v", err)
	}
	empty := &VirtualMemoryStat{}
	if v == empty {
		t.Errorf("error %v", v)
	}

	assert.True(t, v.Total > 0)
	assert.True(t, v.Available > 0)
	assert.True(t, v.Used > 0)

	assert.Equal(t, v.Total, v.Available+v.Used,
		"Total should be computable from available + used: %v", v)

	assert.True(t, v.Free > 0)
	assert.True(t, v.Available > v.Free,
		"Free should be a subset of Available: %v", v)

	assert.InDelta(t, v.UsedPercent,
		100*float64(v.Used)/float64(v.Total), 0.1,
		"UsedPercent should be how many percent of Total is Used: %v", v)
}
Пример #26
0
func TestPointSimilarities(t *testing.T) {
	var examples = []struct {
		closestPoints   []float64
		idealPoints     []float64
		expectedMatches []float64
		fit             float64
	}{
		{
			closestPoints:   preparePointDistances([]float64{2.5, 12, 21.5, 32.5}),
			idealPoints:     []float64{2, 12, 22},
			expectedMatches: []float64{2.5, 12, 21.5},
			fit:             0.9888,
		},
		{
			closestPoints:   preparePointDistances([]float64{2.5, 12, 21.5, 32.5}),
			idealPoints:     []float64{12, 22, 32},
			expectedMatches: []float64{12, 21.5, 32.5},
			fit:             0.9833,
		},
		{
			closestPoints:   preparePointDistances([]float64{0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 140}),
			idealPoints:     []float64{10, 20, 30, 40, 50, 60, 70, 80, 90, 100},
			expectedMatches: []float64{10, 20, 30, 40, 50, 60, 70, 80, 90, 100},
			fit:             1.0,
		},
	}

	for _, tt := range examples {
		fit, matchedPoints := pointSimilarities(tt.idealPoints, tt.closestPoints)
		assert.InDelta(t, tt.fit, fit, 0.0001)
		assert.EqualValues(t, tt.expectedMatches, matchedPoints)
	}
}
Пример #27
0
func TestSettingLength(t *testing.T) {
	assert := assert.New(t)
	v := NewVec3(3, 4, 0)
	assert.InDelta(5, v.Length(), Epsilon, "Vector's length should be 5")
	v.SetLength(10)
	assertEqualVectors(t, NewVec3(6, 8, 0), v)
}
Пример #28
0
func TestDPNBagUpdate(t *testing.T) {
	if runRestTests(t) == false {
		return
	}
	client := getClient(t)
	bag := testutil.MakeDPNBag()
	resp := client.DPNBagCreate(bag)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)

	// We have to set UpdatedAt ahead, or the server won't update
	// record we're sending.
	newTimestamp := time.Now().UTC().Add(1 * time.Second).Truncate(time.Second)
	newLocalId := fmt.Sprintf("GO-TEST-BAG-%s", uuid.NewV4().String())

	dpnBag := resp.Bag()
	dpnBag.UpdatedAt = newTimestamp
	dpnBag.LocalId = newLocalId

	updateResp := client.DPNBagUpdate(dpnBag)
	require.NotNil(t, updateResp)
	require.Nil(t, updateResp.Error)
	updatedBag := updateResp.Bag()
	require.NotNil(t, updatedBag)
	assert.InDelta(t, newTimestamp.Unix(), updatedBag.UpdatedAt.Unix(), float64(2.0))
	assert.Equal(t, newLocalId, updatedBag.LocalId)
}
Пример #29
0
func TestMaxTime(t *testing.T) {
	assert := assert.New(t)
	b, err := New(time.Millisecond*200, 100000, 100000, 10,
		func(str interface{}) uint {
			return uint(len(str.(string)))
		},
	)
	assert.Nil(err)

	go func() {
		for i := 0; i < 10000; i++ {
			b.Put("a")
			time.Sleep(time.Millisecond)
		}
	}()

	before := time.Now()
	batch, err := b.Get()

	// This delta is normally 1-3 ms but running tests in CI with -race causes
	// this to run much slower. For now, just bump up the threshold.
	assert.InDelta(200, time.Since(before).Seconds()*1000, 50)
	assert.True(len(batch) > 0)
	assert.Nil(err)
}
Пример #30
0
func TestStandardDeviationInt(t *testing.T) {
	zeroStdDevList := []int{1, 1, 1, 1, 1}
	assert.Equal(t, 0.0, StandardDeviationInt(zeroStdDevList))

	knownStdDevList := []int{1, 2, 3, 4, 5}
	assert.InDelta(t, 1.5811, StandardDeviationInt(knownStdDevList), 0.0001)
}