/*
 * Copyright 2011 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.debugging.sourcemap;

import static com.google.common.truth.Truth.assertThat;

import junit.framework.TestCase;

/**
 * @author johnlenz@google.com (John Lenz)
 */
public final class Base64VLQTest extends TestCase {
  public void testBase64VLQSelectedValues1() {
    for (int i = 0; i < 63; i++) {
      testValue(i);
    }
  }

  public void testBase64VLQSelectedValues2() {
    int base = 1;
    for (int i = 0; i < 30; i++) {
      testValue(base-1);
      testValue(base);
      base *= 2;
    }
  }

  public void testBase64VLQSelectedSignedValues1() {
    for (int i = -(64*64-1); i < (64*64-1); i++) {
      testValue(i);
    }
  }

  public void testBase64VLQSelectedSignedValues2() {
    int base = 1;
    for (int i = 0; i < 30; i++) {
      testValue(base-1);
      testValue(base);
      base *= 2;
    }
    base = -1;
    for (int i = 0; i < 30; i++) {
      testValue(base-1);
      testValue(base);
      base *= 2;
    }
  }

  static class CharIteratorImpl implements Base64VLQ.CharIterator {
    private int current;
    private int length;
    private CharSequence cs;

    void set(CharSequence sb) {
      this.current = 0;
      this.length = sb.length();
      this.cs = sb;
    }

    @Override
    public boolean hasNext() {
      return current < length;
    }

    @Override
    public char next() {
      return cs.charAt(current++);
    }
  }

  // Disable this test if it is flaky.
  public void testSpeed() {
    long start = System.currentTimeMillis();
    CharIteratorImpl ci = new CharIteratorImpl();
    try {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < 1000000; i++) {
        Base64VLQ.encode(sb, i);
        ci.set(sb);
        int result = Base64VLQ.decode(ci);
        assertThat(result).isEqualTo(i);
        sb.setLength(0);
      }
    } catch (Exception e) {
      throw new RuntimeException("failed.", e);
    }
    long end = System.currentTimeMillis();
    // Was 200ms or less, use a larger number to prevent flakiness
    assertThat(end - start).named("encode+decode time").isLessThan(1000L);
  }

  private void testValue(int value) {
    try {
      StringBuilder sb = new StringBuilder();
      Base64VLQ.encode(sb, value);
      CharIteratorImpl ci = new CharIteratorImpl();
      ci.set(sb);
      int result = Base64VLQ.decode(ci);
      assertThat(result).isEqualTo(value);
    } catch (Exception e) {
      throw new RuntimeException("failed for value " + value, e);
    }
  }


}

@@ -2004,709 +2004,8 @@
  %7D%0A%0A
-  // Disable this test if it is flaky.%0A  public void testSpeed() %7B%0A    long start = System.currentTimeMillis();%0A    CharIteratorImpl ci = new CharIteratorImpl();%0A    try %7B%0A      StringBuilder sb = new StringBuilder();%0A      for (int i = 0; i %3C 1000000; i++) %7B%0A        Base64VLQ.encode(sb, i);%0A        ci.set(sb);%0A        int result = Base64VLQ.decode(ci);%0A        assertThat(result).isEqualTo(i);%0A        sb.setLength(0);%0A      %7D%0A    %7D catch (Exception e) %7B%0A      throw new RuntimeException(%22failed.%22, e);%0A    %7D%0A    long end = System.currentTimeMillis();%0A    // Was 200ms or less, use a larger number to prevent flakiness%0A    assertThat(end - start).named(%22encode+decode time%22).isLessThan(1000L);%0A  %7D%0A
 %0A  p

Remove a test that has been flaky on Travis due to timeouts from the open-source test suite. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=101716223