01: /*
02: * Copyright (C) 2004-2007 Stephen Ostermiller
03: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
04: *
05: * This program is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU General Public License as published by
07: * the Free Software Foundation; either version 2 of the License, or
08: * (at your option) any later version.
09: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * See COPYING.TXT for details.
16: */
17: package com.Ostermiller.util;
18:
19: /**
20: * Significant Figures regression test.
21: */
22: class SignificantFiguresTests {
23:
24: private static class TestCase {
25: private String original;
26: private int sigfigs;
27: private String desiredResult;
28:
29: /**
30: * Test case
31: * @param original test number (as string)
32: * @param sigfigs number of significant figures to use
33: * @param desiredResult desired test result (number as string)
34: */
35: public TestCase(String original, int sigfigs,
36: String desiredResult) {
37: this .original = original;
38: this .sigfigs = sigfigs;
39: this .desiredResult = desiredResult;
40: }
41:
42: private void test() throws Exception {
43: String result = SignificantFigures
44: .format(original, sigfigs);
45: if (!desiredResult.equals(result))
46: throw new Exception("Got " + result + " but expected "
47: + desiredResult + " for " + original + " with "
48: + sigfigs + " significant digits.");
49: }
50: }
51:
52: private static final TestCase[] testCases = new TestCase[] {
53: new TestCase("1234", 3, "1230"),
54: new TestCase("60.91", 3, "60.9"),
55: new TestCase("3343", 1, "3000"),
56: new TestCase("200", 2, "2.0E2"), // not representable without scientific notation
57: new TestCase("5097.808073851760832954355668151943215272",
58: 3, "5.10E3"), // not representable without scientific notation
59: new TestCase("6.15", 2, "6.2"), // rounding on a five - special even odd rule
60: new TestCase("6.25", 2, "6.2"), // rounding on a five - special even odd rule
61: new TestCase("6.150", 2, "6.2"), // rounding on a five - special even odd rule
62: new TestCase("6.250", 2, "6.2"), // rounding on a five - special even odd rule
63: new TestCase("6.1500", 2, "6.2"), // rounding on a five - special even odd rule
64: new TestCase("6.2500", 2, "6.2"), // rounding on a five - special even odd rule
65: new TestCase("6.153", 2, "6.2"), // when more digits special even odd rule does not apply when rounding on a five
66: new TestCase("6.253", 2, "6.3"), // when more digits special even odd rule does not apply when rounding on a five
67: new TestCase("200.123", 3, "200."), // decimal point at end
68: new TestCase("234.123", 3, "234"), // no decimal point at end
69: new TestCase("199.87", 3, "200."), // decimal point at end
70: new TestCase(".0033234324", 2, "0.0033"), // gets leading zero
71: new TestCase("0.0033234324", 2, "0.0033"), // keeps leading zero, can be represented without scientific notation
72: new TestCase(".00033234324", 2, "3.3E-4"), // too small without scientific notation
73: new TestCase("1234567", 3, "1230000"), // can be represented without scientific notation
74: new TestCase("12345678", 3, "1.23E7"), // too large without scientific notation
75: };
76:
77: /**
78: * Main method for regression test
79: * @param args command line arguments (ignored)
80: */
81: public static void main(String[] args) {
82: try {
83: for (int i = 0; i < testCases.length; i++) {
84: testCases[i].test();
85: }
86: } catch (Exception x) {
87: x.printStackTrace(System.err);
88: System.exit(1);
89: }
90: System.exit(0);
91: }
92: }
|