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 java.util.Random;
17  
18  import org.junit.jupiter.api.Assertions;
19  import org.junit.jupiter.api.BeforeEach;
20  
21  import ch.qos.logback.classic.corpus.RandomUtil;
22  import org.junit.jupiter.api.Test;
23  
24  public class RandomUtilTest {
25      long now = System.currentTimeMillis();
26  
27      @BeforeEach
28      public void setup() {
29          System.out.println(RandomUtilTest.class.getName() + " now=" + now);
30      }
31  
32      @Test
33      public void smoke() {
34  
35          int EXPECTED_AVERAGE = 6;
36          int EXPECTED_STD_DEVIATION = 3;
37  
38          System.out.println();
39          Random r = new Random(now);
40          int len = 3000;
41          int[] valArray = new int[len];
42          for (int i = 0; i < len; i++) {
43              valArray[i] = RandomUtil.gaussianAsPositiveInt(r, EXPECTED_AVERAGE, EXPECTED_STD_DEVIATION);
44          }
45          double avg = average(valArray);
46  
47          Assertions.assertEquals(EXPECTED_AVERAGE, avg, 0.3);
48      }
49  
50      public double average(int[] va) {
51          double avg = 0;
52          for (int i = 0; i < va.length; i++) {
53              avg = (avg * i + va[i]) / (i + 1);
54          }
55          return avg;
56      }
57  
58  }