package com.medallia.word2vec;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.junit.After;
import org.junit.Test;

import com.medallia.word2vec.Searcher.UnknownWordException;
import com.medallia.word2vec.util.Common;

/**
 * Tests converting the binary models into
 * {@link com.medallia.word2vec.Word2VecModel}s.
 * 
 * @see com.medallia.word2vec.Word2VecModel#fromBinFile(File)
 * @see com.medallia.word2vec.Word2VecModel#fromBinFile(File,
 *      java.nio.ByteOrder)
 */
public class Word2VecBinTest {

  /**
   * Tests that the Word2VecModels created from a binary and text
   * representations are equivalent
   */
  @Test
  public void testRead()
      throws IOException, UnknownWordException {
    File binFile = Common.getResourceAsFile(
            this.getClass(),
            "/com/medallia/word2vec/tokensModel.bin");
    Word2VecModel binModel = Word2VecModel.fromBinFile(binFile);

    File txtFile = Common.getResourceAsFile(
            this.getClass(),
            "/com/medallia/word2vec/tokensModel.txt");
    Word2VecModel txtModel = Word2VecModel.fromTextFile(txtFile);

    assertModelsEqual(binModel, txtModel);
  }

  private Path tempFile = null;

  /**
   * Tests that a Word2VecModel round-trips through the bin format without changes
   */
  @Test
  public void testRoundTrip() throws IOException, UnknownWordException {
    final String filename = "word2vec.c.output.model.txt";
    final Word2VecModel model =
        Word2VecModel.fromTextFile(filename, Common.readResource(Word2VecTest.class, filename));

    tempFile = Files.createTempFile(
            String.format("%s-", Word2VecBinTest.class.getSimpleName()), ".bin");
    try (final OutputStream os = Files.newOutputStream(tempFile)) {
      model.toBinFile(os);
    }

    final Word2VecModel modelCopy = Word2VecModel.fromBinFile(tempFile.toFile());
    assertModelsEqual(model, modelCopy);
  }

  @After
  public void cleanupTempFile() throws IOException {
    if(tempFile != null)
      Files.delete(tempFile);
  }

  private void assertModelsEqual(
      final Word2VecModel leftModel,
      final Word2VecModel rightModel) throws UnknownWordException {
    final Searcher leftSearcher = leftModel.forSearch();
    final Searcher rightSearcher = rightModel.forSearch();

    // test vocab
    for (String vocab : leftModel.getVocab()) {
      assertTrue(rightSearcher.contains(vocab));
    }
    for (String vocab : rightModel.getVocab()) {
      assertTrue(leftSearcher.contains(vocab));
    }
    // test vector
    for (String vocab : leftModel.getVocab()) {
      final List<Double> leftVector = leftSearcher.getRawVector(vocab);
      final List<Double> rightVector = rightSearcher.getRawVector(vocab);
      assertVectorsEqual(leftVector, rightVector);
    }
  }

  private void assertVectorsEqual(
      final List<Double> leftVector,
      final List<Double> rightVector) {
    assertEquals(leftVector.size(), rightVector.size());
    for (int i = 0; i < leftVector.size(); i++) {
      double txtD = leftVector.get(i);
      double binD = rightVector.get(i);
      assertEquals(txtD, binD, 0.0001);
    }
  }
}

@@ -295,16 +295,41 @@
 .After;%0A
+import org.junit.Assert;%0A
 import o
@@ -1332,27 +1332,22 @@
   assert
-Models
 Equal
+s
 (binMode
@@ -2076,27 +2076,22 @@
   assert
-Models
 Equal
+s
 (model, 
@@ -2254,19 +2254,14 @@
 sert
-Models
 Equal
+s
 (%0A  
@@ -2929,28 +2929,22 @@
   assert
-Vectors
 Equal
+s
 (leftVec
@@ -2998,20 +2998,14 @@
 sert
-Vectors
 Equal
+s
 (%0A  
@@ -3079,24 +3079,31 @@
 ctor) %7B%0A    
+Assert.
 assertEquals
@@ -3270,32 +3270,39 @@
 r.get(i);%0A      
+Assert.
 assertEquals(txt

Renamed the equals methods