KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > SignificantFiguresTests


1 /*
2  * Significant Figures regression test.
3  * Copyright (C) 2004 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.util.*;
21 import java.io.*;
22
23 class SignificantFiguresTests {
24
25     private static class TestCase {
26         private String JavaDoc original;
27         private int sigfigs;
28         private String JavaDoc desiredResult;
29
30         public TestCase(String JavaDoc original, int sigfigs, String JavaDoc desiredResult){
31             this.original = original;
32             this.sigfigs = sigfigs;
33             this.desiredResult = desiredResult;
34         }
35         private void test() throws Exception JavaDoc {
36             String JavaDoc result = SignificantFigures.format(original, sigfigs);
37             if (!desiredResult.equals(result)) throw new Exception JavaDoc("Got " + result + " but expected " + desiredResult + " for " + original + " with " + sigfigs + " significant digits.");
38         }
39     }
40     private static final TestCase[] testCases = new TestCase[]{
41         new TestCase("1234", 3, "1230"),
42         new TestCase("60.91", 3, "60.9"),
43         new TestCase("3343", 1, "3000"),
44         new TestCase("200", 2, "2.0E2"), // not representable without scientific notation
45
new TestCase("5097.808073851760832954355668151943215272", 3, "5.10E3"), // not representable without scientific notation
46
new TestCase("6.15", 2, "6.2"), // rounding on a five - special even odd rule
47
new TestCase("6.25", 2, "6.2"), // rounding on a five - special even odd rule
48
new TestCase("6.150", 2, "6.2"), // rounding on a five - special even odd rule
49
new TestCase("6.250", 2, "6.2"), // rounding on a five - special even odd rule
50
new TestCase("6.1500", 2, "6.2"), // rounding on a five - special even odd rule
51
new TestCase("6.2500", 2, "6.2"), // rounding on a five - special even odd rule
52
new TestCase("6.153", 2, "6.2"), // when more digits special even odd rule does not apply when rounding on a five
53
new TestCase("6.253", 2, "6.3"), // when more digits special even odd rule does not apply when rounding on a five
54
new TestCase("200.123", 3, "200."), // decimal point at end
55
new TestCase("234.123", 3, "234"), // no decimal point at end
56
new TestCase("199.87", 3, "200."), // decimal point at end
57
new TestCase(".0033234324", 2, "0.0033"), // gets leading zero
58
new TestCase("0.0033234324", 2, "0.0033"), // keeps leading zero, can be represented without scientific notation
59
new TestCase(".00033234324", 2, "3.3E-4"), // too small without scientific notation
60
new TestCase("1234567", 3, "1230000"), // can be represented without scientific notation
61
new TestCase("12345678", 3, "1.23E7"), // too large without scientific notation
62
};
63
64     public static void main(String JavaDoc[] args){
65         try {
66             for (int i=0; i<testCases.length; i++){
67                 testCases[i].test();
68             }
69         } catch (Exception JavaDoc x){
70             x.printStackTrace(System.err);
71             System.exit(1);
72         }
73         System.exit(0);
74     }
75 }
76
Popular Tags