1 import java
.io
.PrintWriter
;
2 import java
.io
.FileNotFoundException
;
9 void printTest(String comment
, int[] n
, int[] w
) throws FileNotFoundException
{
11 System
.out
.println("Test " + String
.format("%02d", tn
) + ": " + comment
);
12 PrintWriter out
= new PrintWriter(String
.format("%02d", tn
));
13 int[] p
= new int[n
.length
];
14 for (int i
= 0; i
< n
.length
; i
++) {
15 p
[i
] = (int) Math
.rint(100.0 * (n
[i
] - w
[i
]) / n
[i
]);
19 for (int i
= 0; i
< n
.length
; i
++) {
23 out
.println(kk
+ " " + nn
+ " " + n
.length
);
24 for (int i
= 0; i
< n
.length
; i
++) {
30 Random random
= new Random(124234234);
33 public static void main(String
[] args
) {
36 } catch (Exception e
) {
42 private void run() throws Exception
{
43 printTest("Sample test",
44 new int[] {13, 13, 18, 13, 16, 14},
45 new int[] {0, 0, 3, 1, 4, 1}
48 for (int n
= 1; n
< 4; n
++) {
49 for (int k
= 0; k
<= n
; k
++) {
50 printTest(String
.format("One category, n = %d, k = %d", n
, k
),
56 for (int n1
= 1; n1
< 3; n1
++) {
57 for (int k1
= 0; k1
<= n1
; k1
++) {
58 for (int n2
= n1
; n2
< 3; n2
++) {
59 for (int k2
= 0; k2
<= n2
; k2
++) {
60 printTest(String
.format("Two categories, (%d of %d) and (%d of %d)", k1
, n1
, k2
, n2
),
62 new int[] {n1
- k1
, n2
- k2
}
69 for (int i
= 0; i
< 10; i
++) {
74 for (int j
= 0; j
< m
; j
++) {
77 n
[j
] = random
.nextInt(100) + 1;
78 w
[j
] = random
.nextInt(n
[j
]);
79 z
= 100.0 * w
[j
] / n
[j
];
80 z
= Math
.abs(z
- Math
.round(z
));
81 } while (Math
.abs(z
- 0.5) > 1e-9);
89 int[] n2
= new int[m
];
90 int[] w2
= new int[m
];
91 System
.arraycopy(n
, 0, n2
, 0, m
);
92 System
.arraycopy(w
, 0, w2
, 0, m
);
93 printTest("Rounding issues", n2
, w2
);
96 for (int i
= 0; i
< 10; i
++) {
97 int m
= 5 + random
.nextInt(6);
98 int nn
= 50 + random
.nextInt(51);
99 int kk
= random
.nextInt(nn
+ 1);
100 int[] n
= new int[m
];
104 n
[random
.nextInt(m
)]++;
111 x
= random
.nextInt(m
);
116 printTest("Random test", n
, w
);
121 int[] n
= new int[m
];
123 int[] w
= new int[m
];
125 printTest("100% test", n
, w
);
130 int[] n
= new int[m
];
132 int[] w
= new int[m
];
134 printTest("0% test", n
, w
);
139 int[] n
= new int[m
];
141 int[] w
= new int[m
];
143 printTest("50% test", n
, w
);
146 for (int i
= 0; i
< 10; i
++) {
149 int kk
= random
.nextInt(nn
+ 1);
150 int[] n
= new int[m
];
154 n
[random
.nextInt(m
)]++;
157 int[] w
= new int[m
];
158 for (int j
= 0; j
< m
; j
++) {
159 int k
= random
.nextInt(100);
166 printTest("Random 0, 100 test", n
, w
);
169 for (int i
= 0; i
< 10; i
++) {
172 int kk
= random
.nextInt(nn
+ 1);
173 int[] n
= new int[m
];
177 n
[random
.nextInt(m
)]++;
180 int[] w
= new int[m
];
181 for (int j
= 0; j
< m
; j
++) {
182 int k
= random
.nextInt(100);
184 w
[j
] = random
.nextInt(n
[j
] + 1);
191 printTest("Random 0, 100, x test", n
, w
);
194 for (int i
= 0; i
< 10; i
++) {
197 int kk
= random
.nextInt(nn
+ 1);
198 int[] n
= new int[m
];
202 n
[random
.nextInt(m
)]++;
209 x
= random
.nextInt(m
);
214 printTest("Big random test", n
, w
);