12e5b6d6dSopenharmony_ci// Copyright 2008 the V8 project authors. All rights reserved.
22e5b6d6dSopenharmony_ci// Redistribution and use in source and binary forms, with or without
32e5b6d6dSopenharmony_ci// modification, are permitted provided that the following conditions are
42e5b6d6dSopenharmony_ci// met:
52e5b6d6dSopenharmony_ci//
62e5b6d6dSopenharmony_ci//     * Redistributions of source code must retain the above copyright
72e5b6d6dSopenharmony_ci//       notice, this list of conditions and the following disclaimer.
82e5b6d6dSopenharmony_ci//     * Redistributions in binary form must reproduce the above
92e5b6d6dSopenharmony_ci//       copyright notice, this list of conditions and the following
102e5b6d6dSopenharmony_ci//       disclaimer in the documentation and/or other materials provided
112e5b6d6dSopenharmony_ci//       with the distribution.
122e5b6d6dSopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
132e5b6d6dSopenharmony_ci//       contributors may be used to endorse or promote products derived
142e5b6d6dSopenharmony_ci//       from this software without specific prior written permission.
152e5b6d6dSopenharmony_ci//
162e5b6d6dSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172e5b6d6dSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182e5b6d6dSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192e5b6d6dSopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202e5b6d6dSopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212e5b6d6dSopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222e5b6d6dSopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232e5b6d6dSopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242e5b6d6dSopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252e5b6d6dSopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262e5b6d6dSopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272e5b6d6dSopenharmony_ci
282e5b6d6dSopenharmony_ci#ifndef CCTEST_H_
292e5b6d6dSopenharmony_ci#define CCTEST_H_
302e5b6d6dSopenharmony_ci
312e5b6d6dSopenharmony_ci#include <stdio.h>
322e5b6d6dSopenharmony_ci#include <string.h>
332e5b6d6dSopenharmony_ci#include <inttypes.h>
342e5b6d6dSopenharmony_ci
352e5b6d6dSopenharmony_ci#include "double-conversion/utils.h"
362e5b6d6dSopenharmony_ci
372e5b6d6dSopenharmony_ci#ifndef TEST
382e5b6d6dSopenharmony_ci#define TEST(Name)                                                       \
392e5b6d6dSopenharmony_ci  static void Test##Name();                                              \
402e5b6d6dSopenharmony_ci  CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true);  \
412e5b6d6dSopenharmony_ci  static void Test##Name()
422e5b6d6dSopenharmony_ci#endif
432e5b6d6dSopenharmony_ci
442e5b6d6dSopenharmony_ci#ifndef DEPENDENT_TEST
452e5b6d6dSopenharmony_ci#define DEPENDENT_TEST(Name, Dep)                                        \
462e5b6d6dSopenharmony_ci  static void Test##Name();                                              \
472e5b6d6dSopenharmony_ci  CcTest register_test_##Name(Test##Name, __FILE__, #Name, #Dep, true);  \
482e5b6d6dSopenharmony_ci  static void Test##Name()
492e5b6d6dSopenharmony_ci#endif
502e5b6d6dSopenharmony_ci
512e5b6d6dSopenharmony_ci#ifndef DISABLED_TEST
522e5b6d6dSopenharmony_ci#define DISABLED_TEST(Name)                                              \
532e5b6d6dSopenharmony_ci  static void Test##Name();                                              \
542e5b6d6dSopenharmony_ci  CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, false); \
552e5b6d6dSopenharmony_ci  static void Test##Name()
562e5b6d6dSopenharmony_ci#endif
572e5b6d6dSopenharmony_ci
582e5b6d6dSopenharmony_ci#define CHECK(condition) CheckHelper(__FILE__, __LINE__, #condition, condition)
592e5b6d6dSopenharmony_ci#define CHECK_GE(a, b) CHECK((a) >= (b))
602e5b6d6dSopenharmony_ci
612e5b6d6dSopenharmony_cistatic inline void CheckHelper(const char* file,
622e5b6d6dSopenharmony_ci                               int line,
632e5b6d6dSopenharmony_ci                               const char* source,
642e5b6d6dSopenharmony_ci                               bool condition) {
652e5b6d6dSopenharmony_ci  if (!condition) {
662e5b6d6dSopenharmony_ci    printf("%s:%d:\n CHECK(%s) failed\n", file, line, source);
672e5b6d6dSopenharmony_ci    abort();
682e5b6d6dSopenharmony_ci  }
692e5b6d6dSopenharmony_ci}
702e5b6d6dSopenharmony_ci
712e5b6d6dSopenharmony_ci#define CHECK_EQ(a, b) CheckEqualsHelper(__FILE__, __LINE__, #a, a, #b, b)
722e5b6d6dSopenharmony_ci
732e5b6d6dSopenharmony_citemplate<typename T> inline void PrintfValue(T x);
742e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(int x) { printf("%d", x); }
752e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(unsigned int x) { printf("%u", x); }
762e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(short x) { printf("%hd", x); }
772e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(unsigned short x) { printf("%hu", x); }
782e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(int64_t x) { printf("%" PRId64, x); }
792e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(uint64_t x) { printf("%" PRIu64, x); }
802e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(float x) { printf("%.30e", static_cast<double>(x)); }
812e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(double x) { printf("%.30e", x); }
822e5b6d6dSopenharmony_citemplate<> inline void PrintfValue(bool x) { printf("%s", x ? "true" : "false"); }
832e5b6d6dSopenharmony_ci
842e5b6d6dSopenharmony_citemplate<typename T1, typename T2>
852e5b6d6dSopenharmony_ciinline void CheckEqualsHelper(const char* file, int line,
862e5b6d6dSopenharmony_ci                       const char* expected_source,
872e5b6d6dSopenharmony_ci                       T1 expected,
882e5b6d6dSopenharmony_ci                       const char* value_source,
892e5b6d6dSopenharmony_ci                       T2 value) {
902e5b6d6dSopenharmony_ci  // If expected and value are NaNs then expected != value.
912e5b6d6dSopenharmony_ci  if (expected != value && (expected == expected || value == value)) {
922e5b6d6dSopenharmony_ci    printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n",
932e5b6d6dSopenharmony_ci           file, line, expected_source, value_source);
942e5b6d6dSopenharmony_ci    printf("#  Expected: ");
952e5b6d6dSopenharmony_ci    PrintfValue(expected);
962e5b6d6dSopenharmony_ci    printf("\n");
972e5b6d6dSopenharmony_ci    printf("#  Found:    ");
982e5b6d6dSopenharmony_ci    PrintfValue(value);
992e5b6d6dSopenharmony_ci    printf("\n");
1002e5b6d6dSopenharmony_ci    abort();
1012e5b6d6dSopenharmony_ci  }
1022e5b6d6dSopenharmony_ci}
1032e5b6d6dSopenharmony_ci
1042e5b6d6dSopenharmony_citemplate<>
1052e5b6d6dSopenharmony_ciinline void CheckEqualsHelper(const char* file, int line,
1062e5b6d6dSopenharmony_ci                       const char* expected_source,
1072e5b6d6dSopenharmony_ci                       const char* expected,
1082e5b6d6dSopenharmony_ci                       const char* value_source,
1092e5b6d6dSopenharmony_ci                       const char* value) {
1102e5b6d6dSopenharmony_ci  if ((expected == NULL && value != NULL) ||
1112e5b6d6dSopenharmony_ci      (expected != NULL && value == NULL)) {
1122e5b6d6dSopenharmony_ci    abort();
1132e5b6d6dSopenharmony_ci  }
1142e5b6d6dSopenharmony_ci
1152e5b6d6dSopenharmony_ci  if ((expected != NULL && value != NULL && strcmp(expected, value) != 0)) {
1162e5b6d6dSopenharmony_ci    printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
1172e5b6d6dSopenharmony_ci           "#  Expected: %s\n"
1182e5b6d6dSopenharmony_ci           "#  Found:    %s\n",
1192e5b6d6dSopenharmony_ci           file, line, expected_source, value_source, expected, value);
1202e5b6d6dSopenharmony_ci    abort();
1212e5b6d6dSopenharmony_ci  }
1222e5b6d6dSopenharmony_ci}
1232e5b6d6dSopenharmony_ci
1242e5b6d6dSopenharmony_citemplate<>
1252e5b6d6dSopenharmony_ciinline void CheckEqualsHelper(const char* file, int line,
1262e5b6d6dSopenharmony_ci                       const char* expected_source,
1272e5b6d6dSopenharmony_ci                       const char* expected,
1282e5b6d6dSopenharmony_ci                       const char* value_source,
1292e5b6d6dSopenharmony_ci                       char* value) {
1302e5b6d6dSopenharmony_ci  CheckEqualsHelper(file, line, expected_source, expected, value_source, static_cast<const char*>(value));
1312e5b6d6dSopenharmony_ci}
1322e5b6d6dSopenharmony_ci
1332e5b6d6dSopenharmony_ciclass CcTest {
1342e5b6d6dSopenharmony_ci public:
1352e5b6d6dSopenharmony_ci  typedef void (TestFunction)();
1362e5b6d6dSopenharmony_ci  CcTest(TestFunction* callback, const char* file, const char* name,
1372e5b6d6dSopenharmony_ci         const char* dependency, bool enabled);
1382e5b6d6dSopenharmony_ci  void Run() { callback_(); }
1392e5b6d6dSopenharmony_ci  static int test_count();
1402e5b6d6dSopenharmony_ci  static CcTest* last() { return last_; }
1412e5b6d6dSopenharmony_ci  CcTest* prev() { return prev_; }
1422e5b6d6dSopenharmony_ci  const char* file() const { return file_; }
1432e5b6d6dSopenharmony_ci  const char* name() const { return name_; }
1442e5b6d6dSopenharmony_ci  const char* dependency() const { return dependency_; }
1452e5b6d6dSopenharmony_ci  bool enabled() const { return enabled_; }
1462e5b6d6dSopenharmony_ci private:
1472e5b6d6dSopenharmony_ci  TestFunction* callback_;
1482e5b6d6dSopenharmony_ci  const char* file_;
1492e5b6d6dSopenharmony_ci  const char* name_;
1502e5b6d6dSopenharmony_ci  const char* dependency_;
1512e5b6d6dSopenharmony_ci  bool enabled_;
1522e5b6d6dSopenharmony_ci  static CcTest* last_;
1532e5b6d6dSopenharmony_ci  CcTest* prev_;
1542e5b6d6dSopenharmony_ci};
1552e5b6d6dSopenharmony_ci
1562e5b6d6dSopenharmony_ci#endif  // ifndef CCTEST_H_
157