Add TDD test cases

Add TDD test cases to verify code correctness.

Issue: I6375E
Test: device/host ut, ark standalone build
Signed-off-by: songqi <songqi32@huawei.com>
Change-Id: If2d4d91c50e7a4481ddf10a7f38b11343acb1483
This commit is contained in:
songqi 2022-11-25 12:21:28 +08:00
parent c42a3a8d5f
commit 2d7f29f8e1
6 changed files with 133 additions and 21 deletions

View File

@ -39,6 +39,14 @@ host_unittest_action("LibPandaBaseTest") {
"utf_test.cpp",
]
if (!is_mingw && !is_mac && target_os != "ios") {
sources += [
"genmc/mutex_test_2.cpp",
"unix_file_test.cpp",
"unix_native_stack_test.cpp",
]
}
configs = [
"$ark_root:ark_config",
"$ark_root/libpandabase:arkbase_public_config",

View File

@ -82,20 +82,17 @@ HWTEST_F(BaseMemStatsTest, HeapAllocateTest, testing::ext::TestSize.Level0)
stats.RecordAllocateRaw(1U, SpaceType::SPACE_TYPE_CODE);
ASSERT_EQ(stats.GetAllocated(SpaceType::SPACE_TYPE_CODE), 1U);
stats.RecordAllocateRaw(2U, SpaceType::SPACE_TYPE_OBJECT);
ASSERT_EQ(stats.GetAllocated(SpaceType::SPACE_TYPE_OBJECT), 2U);
stats.RecordAllocateRaw(3U, SpaceType::SPACE_TYPE_HUMONGOUS_OBJECT);
ASSERT_EQ(stats.GetAllocated(SpaceType::SPACE_TYPE_HUMONGOUS_OBJECT), 3U);
stats.RecordAllocateRaw(2U, SpaceType::SPACE_TYPE_INTERNAL);
ASSERT_EQ(stats.GetAllocated(SpaceType::SPACE_TYPE_INTERNAL), 2U);
stats.RecordFreeRaw(4U, SpaceType::SPACE_TYPE_CODE);
ASSERT_EQ(stats.GetFreed(SpaceType::SPACE_TYPE_CODE), 4U);
stats.RecordFreeRaw(5U, SpaceType::SPACE_TYPE_OBJECT);
ASSERT_EQ(stats.GetFreed(SpaceType::SPACE_TYPE_OBJECT), 5U);
stats.RecordFreeRaw(5U, SpaceType::SPACE_TYPE_INTERNAL);
ASSERT_EQ(stats.GetFreed(SpaceType::SPACE_TYPE_INTERNAL), 5U);
ASSERT_EQ(5U, stats.GetAllocatedHeap());
ASSERT_EQ(5U, stats.GetFreedHeap());
ASSERT_EQ(0U, stats.GetAllocatedHeap());
ASSERT_EQ(0U, stats.GetFreedHeap());
}
} // namespace panda

View File

@ -13,17 +13,21 @@
* limitations under the License.
*/
#include <pthread.h>
#pragma clang diagnostic ignored "-Wc11-extensions"
#include <cstdlib>
#include <gtest/gtest.h>
#include <pthread.h>
#define MC_ON
#include "../../../platforms/unix/libpandabase/futex/fmutex.cpp"
// The tests checks mutex try lock
pthread_t pthread_self(void);
// Copy of mutex storage, after complete implementation should totally replace mutex::current_tid
thread_local pthread_t current_tid;
namespace panda::test {
static struct fmutex g_x;
static int g_shared;
@ -35,19 +39,29 @@ static void *ThreadN(void *arg)
do {
ret = MutexLock(&g_x, true);
} while (!ret);
EXPECT_EQ(g_x.recursiveCount, 1);
g_shared = index;
int r = g_shared;
ASSERT(r == index);
EXPECT_TRUE(r == index);
MutexUnlock(&g_x);
EXPECT_EQ(g_x.recursiveCount, 0);
return nullptr;
}
int main()
HWTEST(FMutexTest, TryLockTest, testing::ext::TestSize.Level0)
{
// The tests check mutex try lock
constexpr int N = 2;
MutexInit(&g_x);
pthread_t t[N];
MutexInit(&g_x);
ASSERT_EQ(g_x.recursiveCount, 0);
ASSERT_EQ(g_x.state_and_waiters_, 0);
ASSERT_EQ(g_x.exclusive_owner_, 0U);
ASSERT_FALSE(g_x.recursive_mutex_);
pthread_t t[N];
for (long i = 0u; i < N; i++) {
pthread_create(&t[i], nullptr, ThreadN, reinterpret_cast<void *>(i));
}
@ -56,6 +70,10 @@ int main()
pthread_join(t[i], nullptr);
}
ASSERT_TRUE(MutexTryLockWithSpinning(&g_x));
MutexDestroy(&g_x);
return 0;
ASSERT_FALSE(MutexDoNotCheckOnDeadlock());
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fcntl.h>
#include <gtest/gtest.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "libpandabase/os/file.h"
namespace panda::test {
HWTEST(UnixFileTest, OpenAndGetFlagsTest, testing::ext::TestSize.Level0)
{
remove("./test_openfile.txt");
os::file::File file1 = os::file::Open("./test_openfile.txt", os::file::Mode::READONLY);
ASSERT_FALSE(file1.IsValid());
remove("./test_openfile.txt");
os::file::File file2 = os::file::Open("./test_openfile.txt", os::file::Mode::READWRITE);
ASSERT_FALSE(file2.IsValid());
remove("./test_openfile.txt");
os::file::File file3 = os::file::Open("./test_openfile.txt", os::file::Mode::WRITEONLY);
ASSERT_TRUE(file3.IsValid());
remove("./test_openfile.txt");
os::file::File file4 = os::file::Open("./test_openfile.txt", os::file::Mode::READWRITECREATE);
ASSERT_TRUE(file4.IsValid());
}
} // namespace panda::test

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <fcntl.h>
#include <gtest/gtest.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "libpandabase/os/native_stack.h"
namespace panda::test {
HWTEST(NativeStackTest, ReadAndWriteFileTest, testing::ext::TestSize.Level0)
{
remove("./test_native_stack.txt");
int fd = open("./test_native_stack.txt", O_WRONLY|O_APPEND|O_CREAT, 0777);
ASSERT_NE(fd, -1);
char buff[1024] = "abcdefg";
ASSERT_TRUE(os::native_stack::WriterOsFile(reinterpret_cast<void *>(buff), static_cast<size_t>(strlen(buff)), fd));
std::string result = "";
ASSERT_TRUE(os::native_stack::ReadOsFile("./test_native_stack.txt", &result));
ASSERT_EQ(result, "abcdefg");
close(fd);
}
} // namespace panda::test

View File

@ -409,20 +409,23 @@ HWTEST(Utf, IsValidModifiedUTF8, testing::ext::TestSize.Level0)
const std::vector<uint8_t> v1 {0x31, 0x00};
EXPECT_TRUE(IsValidModifiedUTF8(v1.data()));
const std::vector<uint8_t> v2 {0x9f};
const std::vector<uint8_t> v2 {0x9f, 0x00};
EXPECT_FALSE(IsValidModifiedUTF8(v2.data()));
const std::vector<uint8_t> v3 {0xf7};
const std::vector<uint8_t> v3 {0xf7, 0x00};
EXPECT_FALSE(IsValidModifiedUTF8(v3.data()));
const std::vector<uint8_t> v4 {0xe0};
const std::vector<uint8_t> v4 {0xe0, 0x00};
EXPECT_FALSE(IsValidModifiedUTF8(v4.data()));
const std::vector<uint8_t> v5 {0xd4};
const std::vector<uint8_t> v5 {0xd4, 0x00};
EXPECT_FALSE(IsValidModifiedUTF8(v5.data()));
const std::vector<uint8_t> v6 {0x11, 0x31, 0x00};
EXPECT_TRUE(IsValidModifiedUTF8(v6.data()));
const std::vector<uint8_t> v7 {0xf8, 0x00};
EXPECT_FALSE(IsValidModifiedUTF8(v7.data()));
}
HWTEST(Utf, ConvertMUtf8ToUtf16Pair, testing::ext::TestSize.Level0)