aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StatsTest.java
blob: 6c4b4a4bbde206338ef1a9a8d04dc71a5dd8cd48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) 2019, Matthias Sohn <matthias.sohn@sap.com> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class StatsTest {
	@Test
	public void testStatsTrivial() {
		Stats s = new Stats();
		s.add(1);
		s.add(1);
		s.add(1);
		assertEquals(3, s.count());
		assertEquals(1.0, s.min(), 1E-6);
		assertEquals(1.0, s.max(), 1E-6);
		assertEquals(1.0, s.avg(), 1E-6);
		assertEquals(0.0, s.var(), 1E-6);
		assertEquals(0.0, s.stddev(), 1E-6);
	}

	@Test
	public void testStats() {
		Stats s = new Stats();
		s.add(1);
		s.add(2);
		s.add(3);
		s.add(4);
		assertEquals(4, s.count());
		assertEquals(1.0, s.min(), 1E-6);
		assertEquals(4.0, s.max(), 1E-6);
		assertEquals(2.5, s.avg(), 1E-6);
		assertEquals(1.666667, s.var(), 1E-6);
		assertEquals(1.290994, s.stddev(), 1E-6);
	}

	@Test
	/**
	 * see
	 * https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Example
	 */
	public void testStatsCancellationExample1() {
		Stats s = new Stats();
		s.add(1E8 + 4);
		s.add(1E8 + 7);
		s.add(1E8 + 13);
		s.add(1E8 + 16);
		assertEquals(4, s.count());
		assertEquals(1E8 + 4, s.min(), 1E-6);
		assertEquals(1E8 + 16, s.max(), 1E-6);
		assertEquals(1E8 + 10, s.avg(), 1E-6);
		assertEquals(30, s.var(), 1E-6);
		assertEquals(5.477226, s.stddev(), 1E-6);
	}

	@Test
	/**
	 * see
	 * https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Example
	 */
	public void testStatsCancellationExample2() {
		Stats s = new Stats();
		s.add(1E9 + 4);
		s.add(1E9 + 7);
		s.add(1E9 + 13);
		s.add(1E9 + 16);
		assertEquals(4, s.count());
		assertEquals(1E9 + 4, s.min(), 1E-6);
		assertEquals(1E9 + 16, s.max(), 1E-6);
		assertEquals(1E9 + 10, s.avg(), 1E-6);
		assertEquals(30, s.var(), 1E-6);
		assertEquals(5.477226, s.stddev(), 1E-6);
	}

	@Test
	public void testNoValues() {
		Stats s = new Stats();
		assertTrue(Double.isNaN(s.var()));
		assertTrue(Double.isNaN(s.stddev()));
		assertTrue(Double.isNaN(s.avg()));
		assertTrue(Double.isNaN(s.min()));
		assertTrue(Double.isNaN(s.max()));
		s.add(42.3);
		assertTrue(Double.isNaN(s.var()));
		assertTrue(Double.isNaN(s.stddev()));
		assertEquals(42.3, s.avg(), 1E-6);
		assertEquals(42.3, s.max(), 1E-6);
		assertEquals(42.3, s.min(), 1E-6);
		s.add(42.3);
		assertEquals(0, s.var(), 1E-6);
		assertEquals(0, s.stddev(), 1E-6);
	}
}