Repository: jerryscript-project/jerryscript
File: tests/unit-core/test-container.c (C)

Code before:
    "  var o3 = {}\n"
    "  var wm = new WeakMap()\n"
    "  wm.set(o1, o2)\n"
    "  wm.set(o2, o3)\n"
    "  return o3\n"
    "})()\n"
  );
  jerry_value_t result = jerry_eval (source,
                                     sizeof (source) - 1,
                                     JERRY_PARSE_NO_OPTS);
  TEST_ASSERT (jerry_value_is_object (result));

  jerry_set_object_native_pointer (result, (void *) &global_counter, &native_info);
  jerry_release_value (result);

  global_counter = 0;
  jerry_gc (JERRY_GC_PRESSURE_LOW);
  TEST_ASSERT (global_counter == 1);

  run_eval ("function assert (v) {\n"
            "  if (v !== true)\n"
            "    throw 'Assertion failed!'\n"
            "}");

  run_eval ("function test_values (arr1, arr2) {\n"
            "  assert (arr1.length == arr2.length);\n"
            "  for (let i = 0; i < arr1.length; i++) {\n"
            "    assert (arr1[i] == arr2[i]);\n"
            "  }\n"
            "}\n");

  run_eval ("var entries_check_array = [1, 3.14, 2, true, 3, 'foo'];\n"
            "var values_check_array = [3.14, true, 'foo'];\n"
            "var keys_check_array = [1, 2, 3,];\n"
            "var obj = { x:3, y:'foo'};\n"
            "var b_int = 1n;\n"
            "var object_check_array = [1, obj];\n");

  run_eval ("var map = new Map();\n"
            "map.set(1,3.14);\n"
            "map.set(2,true);\n"
            "map.set(3,'foo');\n"
            "var set = new Set();\n"
            "set.add(3.14);\n"
            "set.add(true);\n"
            "set.add('foo');\n"
            "var obj_map = new Map();\n"
            "obj_map.set(1, obj);\n");

  run_eval ("var result = create_array_from_container (map, true);\n"
            "test_values (result, entries_check_array);");

Review comment (suggestion):
Use `===`. Furthermore, since this code requires ESNEXT, you can also use `Array.isArray()` to check that arr1 is an Array.

Code after:
    "  var wm = new WeakMap()\n"
    "  wm.set(o1, o2)\n"
    "  wm.set(o2, o3)\n"
    "  return o3\n"
    "})()\n"
  );
  jerry_value_t result = jerry_eval (source,
                                     sizeof (source) - 1,
                                     JERRY_PARSE_NO_OPTS);
  TEST_ASSERT (jerry_value_is_object (result));

  jerry_set_object_native_pointer (result, (void *) &global_counter, &native_info);
  jerry_release_value (result);

  global_counter = 0;
  jerry_gc (JERRY_GC_PRESSURE_LOW);
  TEST_ASSERT (global_counter == 1);

  run_eval ("function assert(v) {\n"
            "  if(v !== true)\n"
            "    throw 'Assertion failed!'\n"
            "}");

  run_eval ("function test_values(arr1, arr2) {\n"
            "  assert(Array.isArray(arr1));\n"
            "  assert(arr1.length == arr2.length);\n"
            "  for(let i = 0; i < arr1.length; i++) {\n"
            "    assert(arr1[i] === arr2[i]);\n"
            "  }\n"
            "}\n");

  run_eval ("var map = new Map();\n"
            "map.set(1, 3.14);\n"
            "map.set(2, true);\n"
            "map.set(3, 'foo');\n"
            "var set = new Set();\n"
            "set.add(3.14);\n"
            "set.add(true);\n"
            "set.add('foo');\n"
            "var obj = { x:3, y:'foo'};\n"
            "var b_int = 1n;\n"
            "var obj_bint_map = new Map();\n"
            "obj_bint_map.set(1, obj);\n"
            "obj_bint_map.set(2, b_int);\n");

  run_eval ("var result = create_array_from_container(map, true);\n"
            "test_values(result, [1, 3.14, 2, true, 3, 'foo']);");

  run_eval ("var result = create_array_from_container(set, false);\n"
            "test_values(result, [3.14, true, 'foo']);");