bh_common_test.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "test_helper.h"
  6. #include "gtest/gtest.h"
  7. #include "bh_common.h"
  8. class bh_common_test_suite : public testing::Test
  9. {
  10. protected:
  11. // You should make the members protected s.t. they can be
  12. // accessed from sub-classes.
  13. // virtual void SetUp() will be called before each test is run. You
  14. // should define it if you need to initialize the variables.
  15. // Otherwise, this can be skipped.
  16. virtual void SetUp() {}
  17. // virtual void TearDown() will be called after each test is run.
  18. // You should define it if there is cleanup work to do. Otherwise,
  19. // you don't have to provide it.
  20. //
  21. virtual void TearDown() {}
  22. public:
  23. WAMRRuntimeRAII<512 * 1024> runtime;
  24. };
  25. #define STR_TEST "test"
  26. TEST_F(bh_common_test_suite, wa_strdup)
  27. {
  28. EXPECT_EQ(nullptr, wa_strdup(nullptr));
  29. EXPECT_NE(nullptr, wa_strdup(STR_TEST));
  30. }
  31. TEST_F(bh_common_test_suite, b_strcpy_s)
  32. {
  33. char dest[10] = { 0 };
  34. EXPECT_EQ(0, b_strcpy_s(dest, sizeof(dest), STR_TEST));
  35. // Test abnormal cases.
  36. EXPECT_EQ(-1, b_strcpy_s(nullptr, 0, nullptr));
  37. EXPECT_EQ(-1, b_strcpy_s(dest, sizeof(dest), nullptr));
  38. EXPECT_EQ(-1, b_strcpy_s(dest, 0, STR_TEST));
  39. }
  40. TEST_F(bh_common_test_suite, b_strcat_s)
  41. {
  42. char dest[10] = { 0 };
  43. EXPECT_EQ(0, b_strcat_s(dest, sizeof(dest), STR_TEST));
  44. // Test abnormal cases.
  45. EXPECT_EQ(-1, b_strcat_s(nullptr, 0, nullptr));
  46. EXPECT_EQ(-1, b_strcat_s(dest, sizeof(dest), nullptr));
  47. EXPECT_EQ(-1, b_strcat_s(dest, 0, STR_TEST));
  48. }
  49. TEST_F(bh_common_test_suite, bh_strdup)
  50. {
  51. EXPECT_NE(nullptr, bh_strdup(STR_TEST));
  52. EXPECT_EQ(nullptr, bh_strdup(nullptr));
  53. }
  54. TEST_F(bh_common_test_suite, b_memmove_s)
  55. {
  56. char dest[10] = { 0 };
  57. EXPECT_EQ(0, b_memmove_s(dest, sizeof(dest), STR_TEST, sizeof(STR_TEST)));
  58. // Test abnormal cases.
  59. EXPECT_EQ(0, b_memmove_s(dest, sizeof(dest), STR_TEST, 0));
  60. EXPECT_EQ(0, b_memmove_s(nullptr, sizeof(dest), STR_TEST, 0));
  61. EXPECT_EQ(0, b_memmove_s(dest, sizeof(dest), nullptr, 0));
  62. EXPECT_EQ(-1, b_memmove_s(dest, sizeof(dest), STR_TEST, sizeof(dest) + 1));
  63. }
  64. TEST_F(bh_common_test_suite, b_memcpy_s)
  65. {
  66. char dest[10] = { 0 };
  67. EXPECT_EQ(0, b_memcpy_s(dest, sizeof(dest), STR_TEST, sizeof(STR_TEST)));
  68. // Test abnormal cases.
  69. EXPECT_EQ(0, b_memcpy_s(dest, sizeof(dest), STR_TEST, 0));
  70. EXPECT_EQ(-1,
  71. b_memcpy_s(nullptr, sizeof(dest), STR_TEST, sizeof(STR_TEST)));
  72. EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), nullptr, sizeof(STR_TEST)));
  73. EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), STR_TEST, sizeof(dest) + 1));
  74. }