View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.corpusTest;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import java.util.Random;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  import ch.qos.logback.classic.corpus.RandomUtil;
24  
25  public class RandomUtilTest {
26      long now = System.currentTimeMillis();
27  
28      @Before
29      public void setup() {
30          System.out.println(RandomUtilTest.class.getName() + " now=" + now);
31      }
32  
33      @Test
34      public void smoke() {
35  
36          int EXPECTED_AVERAGE = 6;
37          int EXPECTED_STD_DEVIATION = 3;
38  
39          System.out.println();
40          Random r = new Random(now);
41          int len = 3000;
42          int[] valArray = new int[len];
43          for (int i = 0; i < len; i++) {
44              valArray[i] = RandomUtil.gaussianAsPositiveInt(r, EXPECTED_AVERAGE, EXPECTED_STD_DEVIATION);
45          }
46          double avg = average(valArray);
47  
48          assertEquals(EXPECTED_AVERAGE, avg, 0.3);
49      }
50  
51      public double average(int[] va) {
52          double avg = 0;
53          for (int i = 0; i < va.length; i++) {
54              avg = (avg * i + va[i]) / (i + 1);
55          }
56          return avg;
57      }
58  
59  }