예제 #1
0
/**
 * Check if two literals are equal
 *
 * @return true if equal
 *         false otherwise
 */
bool
lit_literal_equal (literal_t lit1, /**< first literal */
                   literal_t lit2) /**< second literal */
{
  switch (lit2->get_type ())
  {
    case lit_literal_storage_t::LIT_STR:
    {
      return lit_literal_equal_charset_rec (lit1, static_cast<lit_charset_record_t *>(lit2));
    }
    case lit_literal_storage_t::LIT_MAGIC_STR:
    {
      lit_magic_string_id_t magic_str_id = lit_magic_record_get_magic_str_id (lit2);
      return lit_literal_equal_utf8 (lit1,
                                     lit_get_magic_string_utf8 (magic_str_id),
                                     lit_get_magic_string_size (magic_str_id));
    }
    case lit_literal_storage_t::LIT_MAGIC_STR_EX:
    {
      lit_magic_string_ex_id_t magic_str_ex_id = lit_magic_record_ex_get_magic_str_id (lit2);
      return lit_literal_equal_utf8 (lit1,
                                     lit_get_magic_string_ex_utf8 (magic_str_ex_id),
                                     lit_get_magic_string_ex_size (magic_str_ex_id));
    }
    case lit_literal_storage_t::LIT_NUMBER:
    {
      return lit_literal_equal_num (lit1, static_cast<lit_number_record_t *>(lit2)->get_number ());
    }
    default:
    {
      JERRY_UNREACHABLE ();
    }
  }
} /* lit_literal_equal */
예제 #2
0
/**
 * Check if literal contains the string equal to the passed number.
 * Check that literal is a number literal before performing detailed comparison.
 *
 * @return true if equal
 *         false otherwise
 */
bool
lit_literal_equal_type_num (lit_literal_t lit,     /**< literal to check */
                            ecma_number_t num) /**< number to compare with */
{
  if (lit->type != LIT_RECORD_TYPE_NUMBER)
  {
    return false;
  }

  return lit_literal_equal_num (lit, num);
} /* lit_literal_equal_type_num */
예제 #3
0
/**
 * Check if literal contains the string equal to the passed number.
 * Check that literal is a number literal before performing detailed comparison.
 *
 * @return true if equal
 *         false otherwise
 */
bool
lit_literal_equal_type_num (literal_t lit,     /**< literal to check */
                            ecma_number_t num) /**< number to compare with */
{
  if (lit->get_type () != lit_literal_storage_t::LIT_NUMBER)
  {
    return false;
  }

  return lit_literal_equal_num (lit, num);
} /* lit_literal_equal_type_num */
예제 #4
0
/**
 * Check if two literals are equal
 *
 * @return true if equal
 *         false otherwise
 */
bool
lit_literal_equal (lit_literal_t lit1, /**< first literal */
                   lit_literal_t lit2) /**< second literal */
{
  switch (lit2->type)
  {
    case LIT_RECORD_TYPE_CHARSET:
    {
      return lit_literal_equal_charset_rec (lit1, lit2);
    }
    case LIT_RECORD_TYPE_MAGIC_STR:
    {
      lit_magic_string_id_t magic_str_id = lit_magic_literal_get_magic_str_id (lit2);

      return lit_literal_equal_utf8 (lit1,
                                     lit_get_magic_string_utf8 (magic_str_id),
                                     lit_get_magic_string_size (magic_str_id));
    }
    case LIT_RECORD_TYPE_MAGIC_STR_EX:
    {
      lit_magic_string_ex_id_t magic_str_ex_id = lit_magic_literal_get_magic_str_ex_id (lit2);

      return lit_literal_equal_utf8 (lit1,
                                     lit_get_magic_string_ex_utf8 (magic_str_ex_id),
                                     lit_get_magic_string_ex_size (magic_str_ex_id));
    }
    case LIT_RECORD_TYPE_NUMBER:
    {
      ecma_number_t num = lit_number_literal_get_number (lit2);
      return lit_literal_equal_num (lit1, num);
    }
    default:
    {
      JERRY_UNREACHABLE ();
      break;
    }
  }

  JERRY_UNREACHABLE ();
  return 0;
} /* lit_literal_equal */
int
main (int __attr_unused___ argc,
      char __attr_unused___ **argv)
{
  const ecma_char_t *ptrs[test_sub_iters];
  ecma_number_t numbers[test_sub_iters];
  ecma_char_t strings[test_sub_iters][max_characters_in_string + 1];
  ecma_length_t lengths[test_sub_iters];

  mem_init ();
  lit_init ();

  srand ((unsigned int) time (NULL));
  int k = rand ();
  printf ("seed=%d\n", k);
  srand ((unsigned int) k);

  for (uint32_t i = 0; i < test_iters; i++)
  {
    memset (numbers, 0, sizeof (ecma_number_t) * test_sub_iters);
    memset (lengths, 0, sizeof (ecma_length_t) * test_sub_iters);
    memset (ptrs, 0, sizeof (ecma_char_t *) * test_sub_iters);

    for (uint32_t j = 0; j < test_sub_iters; j++)
    {
      int type = rand () % 3;
      if (type == 0)
      {
        lengths[j] = (ecma_length_t) (rand () % max_characters_in_string + 1);
        generate_string (strings[j], lengths[j]);
        lit_create_literal_from_charset (strings[j], lengths[j]);
        strings[j][lengths[j]] = '\0';
        ptrs[j] = strings[j];
        JERRY_ASSERT (ptrs[j]);
      }
      else if (type == 1)
      {
        ecma_magic_string_id_t msi = (ecma_magic_string_id_t) (rand () % ECMA_MAGIC_STRING__COUNT);
        ptrs[j] = ecma_get_magic_string_zt (msi);
        JERRY_ASSERT (ptrs[j]);
        lengths[j] = (ecma_length_t) ecma_zt_string_length (ptrs[j]);
        lit_create_literal_from_charset (ptrs[j], lengths[j]);
      }
      else
      {
        ecma_number_t num = generate_number ();
        lengths[j] = ecma_number_to_zt_string (num, strings[j], max_characters_in_string);
        lit_create_literal_from_num (num);
      }
    }

    // Add empty string
    lit_create_literal_from_charset (NULL, 0);

    for (uint32_t j = 0; j < test_sub_iters; j++)
    {
      literal_t lit1;
      literal_t lit2;
      if (ptrs[j])
      {
        lit1 = lit_find_or_create_literal_from_charset (ptrs[j], lengths[j]);
        lit2 = lit_find_literal_by_charset (ptrs[j], lengths[j]);
        JERRY_ASSERT (lit_literal_equal_zt (lit1, ptrs[j]));
        JERRY_ASSERT (lit_literal_equal_type_zt (lit2, ptrs[j]));
      }
      else
      {
        lit1 = lit_find_or_create_literal_from_num (numbers[j]);
        lit2 = lit_find_literal_by_num (numbers[j]);
        JERRY_ASSERT (lit_literal_equal_num (lit1, numbers[j]));
        JERRY_ASSERT (lit_literal_equal_type_num (lit2, numbers[j]));
      }
      JERRY_ASSERT (lit1);
      JERRY_ASSERT (lit2);
      JERRY_ASSERT (lit1 == lit2);
      JERRY_ASSERT (lit_literal_equal (lit1, lit2));
    }

    // Check empty string exists
    JERRY_ASSERT (lit_find_literal_by_charset (NULL, 0));

    lit_storage.cleanup ();
    JERRY_ASSERT (lit_storage.get_first () == NULL);
  }

  lit_finalize ();
  mem_finalize (true);
  return 0;
}