| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
package com.google.debugging.sourcemap; |
| 31 |
|
| 32 |
import junit.framework.TestCase; |
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 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 |
|
| 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 |
} |