1/* 2 * Copyright JS Foundation and other contributors, http://js.foundation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16extern "C" 17{ 18 #include "ecma-init-finalize.h" 19 #include "jmem.h" 20 #include "ecma-helpers.h" 21 #include "ecma-conversion.h" 22 #include "ecma-exceptions.h" 23 #include "jcontext.h" 24} 25 26#include "jerryscript-port.h" 27#include "jerryscript-port-default.h" 28#include "ecma-globals.h" 29#include "jerryscript.h" 30#include <gtest/gtest.h> 31#include "test-common.h" 32 33/** 34 * Unit test's main function. 35 */ 36class ToIntegerTest : public testing::Test{ 37public: 38 static void SetUpTestCase() 39 { 40 GTEST_LOG_(INFO) << "ToIntegerTest SetUpTestCase"; 41 } 42 43 static void TearDownTestCase() 44 { 45 GTEST_LOG_(INFO) << "ToIntegerTest TearDownTestCase"; 46 } 47 48 void SetUp() override {} 49 void TearDown() override {} 50 51}; 52 53static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 54static void* context_alloc_fn(size_t size, void* cb_data) 55{ 56 (void)cb_data; 57 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 58 return malloc(newSize); 59} 60 61HWTEST_F(ToIntegerTest, Test001, testing::ext::TestSize.Level1) 62{ 63 TEST_INIT (); 64 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 65 jerry_port_default_set_current_context (ctx_p); 66 jmem_init (); 67 ecma_init (); 68 69 ecma_number_t num; 70 71 ecma_value_t int_num = ecma_make_int32_value (123); 72 73 ecma_number_t result = ecma_op_to_integer (int_num, &num); 74 75 ecma_free_value (int_num); 76 77 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 78 TEST_ASSERT (num == 123); 79 80 /* 2 */ 81 ecma_value_t error = ecma_raise_type_error (ECMA_ERR_MSG ("I am a neat little error message")); 82 83 result = ecma_op_to_integer (error, &num); 84 85 jcontext_release_exception (); 86 87 TEST_ASSERT (ECMA_IS_VALUE_ERROR (result)); 88 89 /* 3 */ 90 ecma_value_t nan = ecma_make_nan_value (); 91 92 result = ecma_op_to_integer (nan, &num); 93 94 ecma_free_value (nan); 95 96 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 97 TEST_ASSERT (num == 0); 98 99 /* 4 */ 100 /* -0 */ 101 ecma_value_t negative_zero = ecma_make_number_value (-0.0f); 102 103 result = ecma_op_to_integer (negative_zero, &num); 104 105 ecma_free_value (negative_zero); 106 107 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 108 TEST_ASSERT (1.0f / num == ecma_number_make_infinity (true)); 109 110 /* +0 */ 111 ecma_value_t positive_zero = ecma_make_number_value (+0.0f); 112 113 result = ecma_op_to_integer (positive_zero, &num); 114 115 ecma_free_value (positive_zero); 116 117 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 118 TEST_ASSERT (1.0f / num == ecma_number_make_infinity (false)); 119 120 /* -infinity */ 121 ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true)); 122 123 result = ecma_op_to_integer (negative_infinity, &num); 124 125 ecma_free_value (negative_infinity); 126 127 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 128 TEST_ASSERT (num == ecma_number_make_infinity (true)); 129 130 /* +infinity */ 131 ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false)); 132 133 result = ecma_op_to_integer (positive_infinity, &num); 134 135 ecma_free_value (positive_infinity); 136 137 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 138 TEST_ASSERT (num == ecma_number_make_infinity (false)); 139 140 /* 5 */ 141 ecma_value_t floor = ecma_make_number_value (3.001f); 142 143 result = ecma_op_to_integer (floor, &num); 144 145 ecma_free_value (floor); 146 147 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 148 TEST_ASSERT (num == 3); 149 150 ecma_value_t floor2 = ecma_make_number_value (-26.5973); 151 152 result = ecma_op_to_integer (floor2, &num); 153 154 ecma_free_value (floor2); 155 156 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 157 TEST_ASSERT (num == -26); 158 159 ecma_finalize (); 160 jmem_finalize (); 161 free(ctx_p); 162 return; 163} 164