<commit_before>"use strict";

var caching = require("../src/transit/caching.js"),
    w       = require("../src/transit/writer.js"),
    d       = require("../src/transit/decoder.js"),
    t       = require("../src/transit/types.js"),
    url     = require("url"),
    eq      = require("../src/transit/eq.js");

// =============================================================================
// Equality & Hashing
// =============================================================================

exports.testEquality = function(test) {

  test.ok(eq.equals(1, 1), "1 equals 1");
  test.ok(!eq.equals(1, 2), "1 does not equal 2");
  test.ok(eq.equals("foo", "foo"), "\"foo\" equals \"foo\"");
  test.ok(!eq.equals("foo", "bar"), "\"foo\" does not equal \"bar\"");
  test.ok(eq.equals([], []), "[]  equals []");
  test.ok(eq.equals([1,2,3], [1,2,3]), "[1,2,3] equals [1,2,3]");
  test.ok(!eq.equals([2,2,3], [1,2,3]), "[2,2,3] does not equal [1,2,3]");
  test.ok(eq.equals([1,[2,3],4], [1,[2,3],4]), "[1,[2,3],4] equals [1,[2,3],4]");
  test.ok(!eq.equals([1,[3,3],4], [1,[2,3],4]), "[1,[3,3],4] does not equal [1,[2,3],4]");
  test.ok(!eq.equals([1,2,3], {}), "[1,2,3] does not equal {}");
  test.ok(eq.equals({}, {}), "{} equals {}");
  test.ok(eq.equals({foo: "bar"}, {foo: "bar"}), "{foo: \"bar\"} equals {foo: \"bar\"}");
  test.ok(!eq.equals({foo: "bar", baz: "woz"}, {foo: "bar"}), "{foo: \"bar\", baz: \"woz\"} equals {foo: \"bar\"}");
  test.ok(!eq.equals({foo: "bar"}, {foo: "baz"}), "{foo: \"bar\"} does not equal {foo: \"baz\"}");

  var o  = {foo: "bar", baz: "woz"},
      hc = eq.hashCode(o);

  test.ok(eq.equals(o, {foo: "bar", baz: "woz"}), "{foo: \"bar\", baz: \"woz\"} instances equal even if hashCode added to one instance");

  var o1  = {foo: "bar", baz: "woz"},
      hc1 = eq.hashCode(o1),
      o2  = {foo: "bar", baz: "woz"},
      hc2 = eq.hashCode(o2);

  test.ok(eq.equals(o1, o2), "{foo: \"bar\", baz: \"woz\"} instances equal even if hashCode added to all instances");

  test.done();
}

exports.testHashCode = function(test) {

  test.equal(eq.hashCode("foo"), eq.hashCode("foo"), "hash code for \"foo\" is always the same");
  test.notEqual(eq.hashCode("foo"), eq.hashCode("fop"), "hash code for \"foo\" is not same as \"fop\"");
  test.equal(eq.hashCode([]), 0, "hash code for [] is 0");
  test.equal(eq.hashCode([1,2,3]), eq.hashCode([1,2,3]), "hash code for [1,2,3] is always the same");
  test.notEqual(eq.hashCode([1,2,3]), eq.hashCode([1,2,4]), "hash code for [1,2,3] and [1,2,4] is differed");
  test.equal(eq.hashCode({foo: "bar"}), eq.hashCode({foo: "bar"}), "hash code for {foo: \"bar\"} is always the same");
  test.notEqual(eq.hashCode({foo: "bar"}), eq.hashCode({foo: "baz"}), "hash code for {foo: \"bar\"} and {foo: \"baz\" is not the same");
  test.equal(eq.hashCode({}), eq.hashCode({}), "hash code for {} is always the same");
  test.equal(eq.hashCode(new Date(2014,4,6)), eq.hashCode(new Date(2014,4,6)), "hash code for dates are always the same");

  test.done();
}

// =============================================================================
// TransitMap
// =============================================================================

exports.testTransitMapBasic = function(test) {

  var m0 = t.transitMap([]);

  test.ok(m0.size == 0, "Size of empty TransitMap is 0");

  var m1 = t.transitMap(["foo", "bar"]);

  test.ok(m1.size == 1, "Size of TransitMap from array of two elements is 1");
  test.ok(m1.has("foo"), "TransitMap has expected key");
  test.equal(m1.get("foo"), "bar", "Accessing key of TransitMap returns expected result");

  var m2 = t.transitMap(["foo", "bar", 101574, "baz"]);

  test.ok(m2.size == 1, "Size of TransitMap with collisions from array of two elements is 2");
  test.ok(m2.has("foo") && m2.has(101574), "TransitMap with collisions has expected keys");
  test.ok((m2.get("foo") == "bar") && (m2.get(101574) == "baz"), "Accessing keys of TransitMap with collisions returns expected result");

  var m3 = t.transitMap(["foo", "bar"]);

  test.equal(eq.hashCode(m1), eq.hashCode(m3), "hash codes for equivalent TransitMaps are always the same");

  var m4 = t.transitMap(["foo", "bop"]);

  test.notEqual(eq.hashCode(m3), eq.hashCode(m4), "TransmitMap({foo: \"bar\"}) and TransmitMap({foo: \"bop\") have different hash codes");

  var m5 = t.transitMap([[1,2], "foo", [3,4], "bar"]);

  test.ok(m5.get([1,2]) === "foo" && (m5.get([3,4]) === "bar"), "Can access complex keys from TransitMap");

  return test.done();
};

// =============================================================================
// Decoding
// =============================================================================

// -----------------------------------------------------------------------------
// Implementation Tests
<commit_msg>fix broken transit map test<commit_after>"use strict";

var caching = require("../src/transit/caching.js"),
    w       = require("../src/transit/writer.js"),
    d       = require("../src/transit/decoder.js"),
    t       = require("../src/transit/types.js"),
    url     = require("url"),
    eq      = require("../src/transit/eq.js");

// =============================================================================
// Equality & Hashing
// =============================================================================

exports.testEquality = function(test) {

  test.ok(eq.equals(1, 1), "1 equals 1");
  test.ok(!eq.equals(1, 2), "1 does not equal 2");
  test.ok(eq.equals("foo", "foo"), "\"foo\" equals \"foo\"");
  test.ok(!eq.equals("foo", "bar"), "\"foo\" does not equal \"bar\"");
  test.ok(eq.equals([], []), "[]  equals []");
  test.ok(eq.equals([1,2,3], [1,2,3]), "[1,2,3] equals [1,2,3]");
  test.ok(!eq.equals([2,2,3], [1,2,3]), "[2,2,3] does not equal [1,2,3]");
  test.ok(eq.equals([1,[2,3],4], [1,[2,3],4]), "[1,[2,3],4] equals [1,[2,3],4]");
  test.ok(!eq.equals([1,[3,3],4], [1,[2,3],4]), "[1,[3,3],4] does not equal [1,[2,3],4]");
  test.ok(!eq.equals([1,2,3], {}), "[1,2,3] does not equal {}");
  test.ok(eq.equals({}, {}), "{} equals {}");
  test.ok(eq.equals({foo: "bar"}, {foo: "bar"}), "{foo: \"bar\"} equals {foo: \"bar\"}");
  test.ok(!eq.equals({foo: "bar", baz: "woz"}, {foo: "bar"}), "{foo: \"bar\", baz: \"woz\"} equals {foo: \"bar\"}");
  test.ok(!eq.equals({foo: "bar"}, {foo: "baz"}), "{foo: \"bar\"} does not equal {foo: \"baz\"}");

  var o  = {foo: "bar", baz: "woz"},
      hc = eq.hashCode(o);

  test.ok(eq.equals(o, {foo: "bar", baz: "woz"}), "{foo: \"bar\", baz: \"woz\"} instances equal even if hashCode added to one instance");

  var o1  = {foo: "bar", baz: "woz"},
      hc1 = eq.hashCode(o1),
      o2  = {foo: "bar", baz: "woz"},
      hc2 = eq.hashCode(o2);

  test.ok(eq.equals(o1, o2), "{foo: \"bar\", baz: \"woz\"} instances equal even if hashCode added to all instances");

  test.done();
}

exports.testHashCode = function(test) {

  test.equal(eq.hashCode("foo"), eq.hashCode("foo"), "hash code for \"foo\" is always the same");
  test.notEqual(eq.hashCode("foo"), eq.hashCode("fop"), "hash code for \"foo\" is not same as \"fop\"");
  test.equal(eq.hashCode([]), 0, "hash code for [] is 0");
  test.equal(eq.hashCode([1,2,3]), eq.hashCode([1,2,3]), "hash code for [1,2,3] is always the same");
  test.notEqual(eq.hashCode([1,2,3]), eq.hashCode([1,2,4]), "hash code for [1,2,3] and [1,2,4] is differed");
  test.equal(eq.hashCode({foo: "bar"}), eq.hashCode({foo: "bar"}), "hash code for {foo: \"bar\"} is always the same");
  test.notEqual(eq.hashCode({foo: "bar"}), eq.hashCode({foo: "baz"}), "hash code for {foo: \"bar\"} and {foo: \"baz\" is not the same");
  test.equal(eq.hashCode({}), eq.hashCode({}), "hash code for {} is always the same");
  test.equal(eq.hashCode(new Date(2014,4,6)), eq.hashCode(new Date(2014,4,6)), "hash code for dates are always the same");

  test.done();
}

// =============================================================================
// TransitMap
// =============================================================================

exports.testTransitMapBasic = function(test) {

  var m0 = t.transitMap([]);

  test.ok(m0.size == 0, "Size of empty TransitMap is 0");

  var m1 = t.transitMap(["foo", "bar"]);

  test.ok(m1.size == 1, "Size of TransitMap from array of two elements is 1");
  test.ok(m1.has("foo"), "TransitMap has expected key");
  test.equal(m1.get("foo"), "bar", "Accessing key of TransitMap returns expected result");

  var m2 = t.transitMap(["foo", "bar", 101574, "baz"]);

  test.ok(m2.size == 2, "Size of TransitMap with collisions from array of two elements is 2");
  test.ok(m2.has("foo") && m2.has(101574), "TransitMap with collisions has expected keys");
  test.ok((m2.get("foo") == "bar") && (m2.get(101574) == "baz"), "Accessing keys of TransitMap with collisions returns expected result");

  var m3 = t.transitMap(["foo", "bar"]);

  test.equal(eq.hashCode(m1), eq.hashCode(m3), "hash codes for equivalent TransitMaps are always the same");

  var m4 = t.transitMap(["foo", "bop"]);

  test.notEqual(eq.hashCode(m3), eq.hashCode(m4), "TransmitMap({foo: \"bar\"}) and TransmitMap({foo: \"bop\") have different hash codes");

  var m5 = t.transitMap([[1,2], "foo", [3,4], "bar"]);

  test.ok(m5.get([1,2]) === "foo" && (m5.get([3,4]) === "bar"), "Can access complex keys from TransitMap");

  return test.done();
};

// =============================================================================
// Decoding
// =============================================================================

// -----------------------------------------------------------------------------
// Implementation Tests

{'fraction_non_alphanumeric': 0.341301184784868, 'fraction_numerical': 0.03450426106838495, 'mean_word_length': 4.115895800106326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1622302', 'n_tokens_mistral': 3585, 'n_tokens_neox': 3352, 'n_words': 810}