<commit_before>/**
 *    Copyright 2014 Galois, Inc.
 *
 * 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.galois.qrstream.qrpipe;


import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.galois.qrstream.image.BitmapImage;
import com.google.common.base.Charsets;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.decoder.Version;

public class TransmitTest {

  private final Transmit transmitter = new Transmit(350,350);

  @BeforeClass
  public static void testSetup() {}

  @AfterClass
  public static void testCleanup() {}

  /**
   * Encoding of QR code with no data should yield no BitmapImage.
   * @throws WriterException
   */
  @Test
  public void testEncodeQRCodesNoData() throws TransmitException {
    // Setup encoding parameters
    Version qrVersion = Version.getVersionForNumber(1);
    ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L;

    byte[] noByteData = new byte[0];

    Iterable<BitmapImage> qrCodes =
        encodeQRAndCheckNotNull(noByteData, qrVersion, ecLevel);

    int size = 0;
    for(@SuppressWarnings("unused") BitmapImage c : qrCodes) {
       size++;
    }
    assertEquals("Empty transmission should yield no QR code", 0, size);
  }

  /**
   * Ensure that we encoded sequence of QR codes correctly
   * @throws IOException
   * @throws WriterException
   */
  @Test
  public void testEncodeQRCodes() throws TransmitException, IOException {

    // Input for test
    String filename = "random2kfile";
    byte[] expectedBytes = UtilsTest.getTextResourceAndCheckNotNull(filename);

    testRoundTripWithPureBarcodeHint(expectedBytes);
    testRoundTripWithNoBarcodeHint(expectedBytes);
  }

  /**
   * Check that ZXing will round trip the input bytes when we send
   * the hint that resulting encoded image contains only a pure QR code.
   * @param expectedBytes the input byte array
   * @throws TransmitException
   */
  private void testRoundTripWithPureBarcodeHint(byte[] expectedBytes) throws TransmitException {
    // ZXing can incorrectly identify black blobs in image as finder
    // square and render it invalid code: https://code.google.com/p/zxing/issues/detail?id=1262
    // The suggestion was to tell ZXing when you just have QR code in image and nothing else.
    Map<DecodeHintType, Object> hints = Receive.getDecodeHints();
    hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

    ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L;
    int errorsDetectingQRCodes=0;
    for (int i=1; i<=40; i++) {
      Version qrVersion = Version.getVersionForNumber(i);
      errorsDetectingQRCodes += testRoundTrip(qrVersion, ecLevel, hints, expectedBytes);
    }
    assertEquals("Expect no problems detecting generated QR codes", 0, errorsDetectingQRCodes);
  }

  /**
   * Check that ZXing will round trip the input bytes when we do not send<commit_msg>a failing unit test for square dimentions<commit_after>/**
 *    Copyright 2014 Galois, Inc.
 *
 * 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.galois.qrstream.qrpipe;


import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.galois.qrstream.image.BitmapImage;
import com.google.common.base.Charsets;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.decoder.Version;

public class TransmitTest {

  private final Transmit transmitter = new Transmit(350,350);

  @BeforeClass
  public static void testSetup() {}

  @AfterClass
  public static void testCleanup() {}

  /**
   * Encoding of QR code with no data should yield no BitmapImage.
   * @throws WriterException
   */
  @Test
  public void testEncodeQRCodesNoData() throws TransmitException {
    // Setup encoding parameters
    Version qrVersion = Version.getVersionForNumber(1);
    ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L;

    byte[] noByteData = new byte[0];

    Iterable<BitmapImage> qrCodes =
        encodeQRAndCheckNotNull(noByteData, qrVersion, ecLevel);

    int size = 0;
    for(@SuppressWarnings("unused") BitmapImage c : qrCodes) {
       size++;
    }
    assertEquals("Empty transmission should yield no QR code", 0, size);
  }

  /**
   * Ensure that we encoded sequence of QR codes correctly
   * @throws IOException
   * @throws WriterException
   */
  @Test
  public void testEncodeQRCodes() throws TransmitException, IOException {

    // Input for test
    String filename = "random2kfile";
    byte[] expectedBytes = UtilsTest.getTextResourceAndCheckNotNull(filename);

    testRoundTripWithPureBarcodeHint(expectedBytes);
    testRoundTripWithNoBarcodeHint(expectedBytes);
  }

  @Test
  public void testSquareDimentions() throws TransmitException {
    Transmit rectangleTransmit = new Transmit(100,200);
    Version qrVersion = Version.getVersionForNumber(1);
    ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L;

    byte[] data = "Square".getBytes();
    Iterable<BitmapImage> qrCodes =
      rectangleTransmit.encodeQRCodes(data, qrVersion, ecLevel);

    BitmapImage image = qrCodes.iterator().next();
    assertEquals("Image dimentions are not square", true, image.getWidth() == image.getHeight() );
  }

  /**
   * Check that ZXing will round trip the input bytes when we send
   * the hint that resulting encoded image contains only a pure QR code.
   * @param expectedBytes the input byte array
   * @throws TransmitException
   */
  private void testRoundTripWithPureBarcodeHint(byte[] expectedBytes) throws TransmitException {
    // ZXing can incorrectly identify black blobs in image as finder
    // square and render it invalid code: https://code.google.com/p/zxing/issues/detail?id=1262
    // The suggestion was to tell ZXing when you just have QR code in image and nothing else.
    Map<DecodeHintType, Object> hints = Receive.getDecodeHints();
    hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

    ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L;
    int errorsDetectingQRCodes=0;
    for (int i=1; i<=40; i++) {
      Version qrVersion = Version.getVersionForNumber(i);
      errorsDetectingQRCodes += testRoundTrip(qrVersion, ecLevel, hints, expectedBytes);
    }
    assertEquals("Expect no problems detecting generated QR codes", 0, errorsDetectingQRCodes);
  }

  /**
   * Check that ZXing will round trip the input bytes when we do not send
{'fraction_non_alphanumeric': 0.08750282997509622, 'fraction_numerical': 0.007357935250169798, 'mean_word_length': 4.2371072910492, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6963451', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2531, 'n_words': 768}