blob: 23a87b662f400065e493ac53a78ddcbda9e8a688 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @summary Test that SnmpTimeTicks wraps around when it is passed a long
27 * value
28 * @bug 4955105
29 * @build TimeTicksWrapping
30 * @run main TimeTicksWrapping
31 */
32import com.sun.jmx.snmp.SnmpTimeticks;
33import com.sun.jmx.snmp.SnmpUnsignedInt;
34
35public class TimeTicksWrapping {
36 public static final long[] oks = {
37 0L, 1L, (long)Integer.MAX_VALUE, (long)Integer.MAX_VALUE*2,
38 (long)Integer.MAX_VALUE*2+1L, (long)Integer.MAX_VALUE*2+2L,
39 (long)Integer.MAX_VALUE*3,
40 SnmpUnsignedInt.MAX_VALUE, SnmpUnsignedInt.MAX_VALUE+1L,
41 SnmpUnsignedInt.MAX_VALUE*3-1L, Long.MAX_VALUE
42 };
43
44 public static final long[] kos = {
45 -1L, (long)Integer.MIN_VALUE, (long)Integer.MIN_VALUE*2,
46 (long)Integer.MIN_VALUE*2-1L, (long)Integer.MIN_VALUE*3,
47 -SnmpUnsignedInt.MAX_VALUE, -(SnmpUnsignedInt.MAX_VALUE+1L),
48 -(SnmpUnsignedInt.MAX_VALUE*3-1L), Long.MIN_VALUE
49 };
50
51 public static void main(String args[]) {
52 try {
53 SnmpTimeticks t;
54
55 for (int i=0;i<oks.length;i++) {
56 final long t1,t2,t3;
57 t1 = (new SnmpTimeticks(oks[i])).longValue();
58 t2 = (new SnmpTimeticks(new Long(oks[i]))).longValue();
59 t3 = oks[i]%0x0100000000L;
60 if (t1 != t3)
61 throw new Exception("Value should have wrapped: " +
62 oks[i] + " expected: " + t3);
63 if (t2 != t3)
64 throw new Exception("Value should have wrapped: " +
65 "Long("+oks[i]+") expected: " + t3);
66
67 if (t1 > SnmpUnsignedInt.MAX_VALUE)
68 throw new Exception("Value should have wrapped " +
69 "for " + oks[i] + ": " +
70 t1 + " exceeds max: " +
71 SnmpUnsignedInt.MAX_VALUE);
72 if (t2 > SnmpUnsignedInt.MAX_VALUE)
73 throw new Exception("Value should have wrapped " +
74 "for " + oks[i] + ": " +
75 t2 + " exceeds max: " +
76 SnmpUnsignedInt.MAX_VALUE);
77
78 if (t1 < 0)
79 throw new Exception("Value should have wrapped: " +
80 "for " + oks[i] + ": " +
81 t1 + " is negative");
82 if (t2 < 0)
83 throw new Exception("Value should have wrapped: " +
84 "for " + oks[i] + ": " +
85 t2 + " is negative");
86
87 System.out.println("TimeTicks[" + oks[i] +
88 "] rightfully accepted: " + t3);
89 }
90
91 for (int i=0;i<kos.length;i++) {
92 try {
93 t = new SnmpTimeticks(kos[i]);
94 throw new Exception("Value should have been rejected: " +
95 kos[i]);
96 } catch (IllegalArgumentException x) {
97 // OK!
98 }
99 try {
100 t = new SnmpTimeticks(new Long(kos[i]));
101 throw new Exception("Value should have been rejected: " +
102 "Long("+kos[i]+")");
103 } catch (IllegalArgumentException x) {
104 // OK!
105 }
106
107 System.out.println("TimeTicks[" + kos[i] +
108 "] rightfully rejected.");
109 }
110
111 } catch(Exception x) {
112 x.printStackTrace();
113 System.exit(1);
114 }
115 }
116}