Debian Codegraph @

test/com/google/debugging/sourcemap/Base64VLQTest.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors. All rights reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of Google Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 package com.google.debugging.sourcemap;
31
32 import junit.framework.TestCase;
33
34 /**
35 * @author [email protected] (John Lenz)
36 */
37 public class Base64VLQTest extends TestCase {
38 public void testBase64VLQSelectedValues1() {
39 for (int i = 0; i < 63; i++) {
40 testValue(i);
41 }
42 }
43
44 public void testBase64VLQSelectedValues2() {
45 int base = 1;
46 for (int i = 0; i < 30; i++) {
47 testValue(base-1);
48 testValue(base);
49 base *= 2;
50 }
51 }
52
53 public void testBase64VLQSelectedSignedValues1() {
54 for (int i = -(64*64-1); i < (64*64-1); i++) {
55 testValue(i);
56 }
57 }
58
59 public void testBase64VLQSelectedSignedValues2() {
60 int base = 1;
61 for (int i = 0; i < 30; i++) {
62 testValue(base-1);
63 testValue(base);
64 base *= 2;
65 }
66 base = -1;
67 for (int i = 0; i < 30; i++) {
68 testValue(base-1);
69 testValue(base);
70 base *= 2;
71 }
72 }
73
74 static class CharIteratorImpl implements Base64VLQ.CharIterator {
75 private int current;
76 private int length;
77 private CharSequence cs;
78
79 void set(CharSequence sb) {
80 this.current = 0;
81 this.length = sb.length();
82 this.cs = sb;
83 }
84
85 @Override
86 public boolean hasNext() {
87 return current < length;
88 }
89
90 @Override
91 public char next() {
92 return cs.charAt(current++);
93 }
94 }
95
96 // Disable this test if it is flaky.
97 public void testSpeed() {
98 long start = System.currentTimeMillis();
99 CharIteratorImpl ci = new CharIteratorImpl();
100 try {
101 StringBuilder sb = new StringBuilder();
102 for (int i = 0; i < 1000000; i++) {
103 Base64VLQ.encode(sb, i);
104 ci.set(sb);
105 int result = Base64VLQ.decode(ci);
106 assertEquals(i, result);
107 sb.setLength(0);
108 }
109 } catch (Exception e) {
110 throw new RuntimeException("failed.", e);
111 }
112 long end = System.currentTimeMillis();
113 // Was 200ms or less, use a larger number to prevent flakiness
114 assertTrue("too slow", end-start < 1000);
115 }
116
117 private void testValue(int value) {
118 try {
119 StringBuilder sb = new StringBuilder();
120 Base64VLQ.encode(sb, value);
121 CharIteratorImpl ci = new CharIteratorImpl();
122 ci.set(sb);
123 int result = Base64VLQ.decode(ci);
124 assertEquals(value, result);
125 } catch (Exception e) {
126 throw new RuntimeException("failed for value " + value, e);
127 }
128 }
129
130
131 }