Refactor Kernel FS tests

Change-Id: Ic8c403bf02a080c12d199bb44d30c694e600f351
Reviewed-on: https://review.haiku-os.org/c/haiku/+/10674
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Kacper Kasper
2026-04-07 13:16:46 +00:00
parent 71fc6a97b4
commit 499154375f
3 changed files with 312 additions and 404 deletions
+57 -107
View File
@@ -4,15 +4,14 @@
*/ */
#include "KPathTest.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <fs/KPath.h> #include <fs/KPath.h>
#include <cppunit/TestCaller.h> #include <TestSuiteAddon.h>
#include <cppunit/TestSuite.h> #include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
typedef void* mutex; typedef void* mutex;
@@ -73,22 +72,25 @@ _mutex_unlock(mutex* lock)
} }
// #pragma mark - class KPathTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(KPathTest);
CPPUNIT_TEST(TestSetToAndPath);
CPPUNIT_TEST(TestLazyAlloc);
CPPUNIT_TEST(TestLeaf);
CPPUNIT_TEST(TestReplaceLeaf);
CPPUNIT_TEST(TestRemoveLeaf);
CPPUNIT_TEST(TestAdopt);
CPPUNIT_TEST(TestLockBuffer);
CPPUNIT_TEST(TestDetachBuffer);
CPPUNIT_TEST(TestNormalize);
CPPUNIT_TEST(TestAssign);
CPPUNIT_TEST(TestEquals);
CPPUNIT_TEST(TestNotEquals);
CPPUNIT_TEST_SUITE_END();
public:
KPathTest::KPathTest() void TestSetToAndPath()
{ {
}
KPathTest::~KPathTest()
{
}
void
KPathTest::TestSetToAndPath()
{
KPath path; KPath path;
status_t status = path.InitCheck(); status_t status = path.InitCheck();
CPPUNIT_ASSERT_MESSAGE("1. ", status == B_OK); CPPUNIT_ASSERT_MESSAGE("1. ", status == B_OK);
@@ -118,12 +120,10 @@ KPathTest::TestSetToAndPath()
status = path.SetTo(NULL, KPath::DEFAULT, SIZE_MAX); status = path.SetTo(NULL, KPath::DEFAULT, SIZE_MAX);
CPPUNIT_ASSERT(status == B_NO_MEMORY); CPPUNIT_ASSERT(status == B_NO_MEMORY);
} }
void TestLazyAlloc()
void {
KPathTest::TestLazyAlloc()
{
KPath path(NULL, KPath::LAZY_ALLOC); KPath path(NULL, KPath::LAZY_ALLOC);
CPPUNIT_ASSERT(path.Path() == NULL); CPPUNIT_ASSERT(path.Path() == NULL);
CPPUNIT_ASSERT(path.Length() == 0); CPPUNIT_ASSERT(path.Length() == 0);
@@ -147,12 +147,10 @@ KPathTest::TestLazyAlloc()
status = path.SetPath("test"); status = path.SetPath("test");
CPPUNIT_ASSERT(status == B_NO_MEMORY); CPPUNIT_ASSERT(status == B_NO_MEMORY);
CPPUNIT_ASSERT(path.InitCheck() == B_NO_MEMORY); CPPUNIT_ASSERT(path.InitCheck() == B_NO_MEMORY);
} }
void TestLeaf()
void {
KPathTest::TestLeaf()
{
KPath path("a"); KPath path("a");
CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0); CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0);
CPPUNIT_ASSERT(strcmp(path.Leaf(), "a") == 0); CPPUNIT_ASSERT(strcmp(path.Leaf(), "a") == 0);
@@ -176,12 +174,10 @@ KPathTest::TestLeaf()
path.SetTo("/a/b//c"); path.SetTo("/a/b//c");
CPPUNIT_ASSERT(strcmp(path.Path(), "/a/b//c") == 0); CPPUNIT_ASSERT(strcmp(path.Path(), "/a/b//c") == 0);
CPPUNIT_ASSERT(strcmp(path.Leaf(), "c") == 0); CPPUNIT_ASSERT(strcmp(path.Leaf(), "c") == 0);
} }
void TestReplaceLeaf()
void {
KPathTest::TestReplaceLeaf()
{
KPath path; KPath path;
status_t status = path.ReplaceLeaf("x"); status_t status = path.ReplaceLeaf("x");
CPPUNIT_ASSERT(status == B_OK); CPPUNIT_ASSERT(status == B_OK);
@@ -203,12 +199,10 @@ KPathTest::TestReplaceLeaf()
CPPUNIT_ASSERT(status == B_OK); CPPUNIT_ASSERT(status == B_OK);
CPPUNIT_ASSERT(path.Length() == 2); CPPUNIT_ASSERT(path.Length() == 2);
CPPUNIT_ASSERT(strcmp(path.Path(), "/c") == 0); CPPUNIT_ASSERT(strcmp(path.Path(), "/c") == 0);
} }
void TestRemoveLeaf()
void {
KPathTest::TestRemoveLeaf()
{
KPath path; KPath path;
bool removed = path.RemoveLeaf(); bool removed = path.RemoveLeaf();
CPPUNIT_ASSERT(!removed); CPPUNIT_ASSERT(!removed);
@@ -239,12 +233,10 @@ KPathTest::TestRemoveLeaf()
CPPUNIT_ASSERT_MESSAGE("5. !removed", !removed); CPPUNIT_ASSERT_MESSAGE("5. !removed", !removed);
CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0); CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0);
CPPUNIT_ASSERT(path.Length() == 1); CPPUNIT_ASSERT(path.Length() == 1);
} }
void TestAdopt()
void {
KPathTest::TestAdopt()
{
KPath one("first", false, 10); KPath one("first", false, 10);
CPPUNIT_ASSERT(one.InitCheck() == B_OK); CPPUNIT_ASSERT(one.InitCheck() == B_OK);
CPPUNIT_ASSERT(one.BufferSize() == 10); CPPUNIT_ASSERT(one.BufferSize() == 10);
@@ -275,12 +267,10 @@ KPathTest::TestAdopt()
CPPUNIT_ASSERT(one.Path() != NULL); CPPUNIT_ASSERT(one.Path() != NULL);
CPPUNIT_ASSERT(strcmp(one.Path(), "test") == 0); CPPUNIT_ASSERT(strcmp(one.Path(), "test") == 0);
CPPUNIT_ASSERT(one.Length() == 4); CPPUNIT_ASSERT(one.Length() == 4);
} }
void TestLockBuffer()
void {
KPathTest::TestLockBuffer()
{
KPath path; KPath path;
CPPUNIT_ASSERT(path.Path() != NULL); CPPUNIT_ASSERT(path.Path() != NULL);
CPPUNIT_ASSERT(path.Length() == 0); CPPUNIT_ASSERT(path.Length() == 0);
@@ -307,12 +297,10 @@ KPathTest::TestLockBuffer()
CPPUNIT_ASSERT(third.Length() == 0); CPPUNIT_ASSERT(third.Length() == 0);
third.UnlockBuffer(); third.UnlockBuffer();
CPPUNIT_ASSERT(third.Length() == 4); CPPUNIT_ASSERT(third.Length() == 4);
} }
void TestDetachBuffer()
void {
KPathTest::TestDetachBuffer()
{
KPath path("test"); KPath path("test");
CPPUNIT_ASSERT(path.InitCheck() == B_OK); CPPUNIT_ASSERT(path.InitCheck() == B_OK);
@@ -322,12 +310,10 @@ KPathTest::TestDetachBuffer()
CPPUNIT_ASSERT(path.Path() == NULL); CPPUNIT_ASSERT(path.Path() == NULL);
CPPUNIT_ASSERT(path.InitCheck() == B_NO_INIT); CPPUNIT_ASSERT(path.InitCheck() == B_NO_INIT);
} }
void TestNormalize()
void {
KPathTest::TestNormalize()
{
// Outside the kernel, we only test the error case. // Outside the kernel, we only test the error case.
KPath path("test/../out"); KPath path("test/../out");
CPPUNIT_ASSERT(path.InitCheck() == B_OK); CPPUNIT_ASSERT(path.InitCheck() == B_OK);
@@ -343,12 +329,10 @@ KPathTest::TestNormalize()
CPPUNIT_ASSERT(path.Path() != NULL); CPPUNIT_ASSERT(path.Path() != NULL);
CPPUNIT_ASSERT(path.Path()[0] == '\0'); CPPUNIT_ASSERT(path.Path()[0] == '\0');
CPPUNIT_ASSERT(path.Path() == path.Leaf()); CPPUNIT_ASSERT(path.Path() == path.Leaf());
} }
void TestAssign()
void {
KPathTest::TestAssign()
{
KPath one("first", false, 10); KPath one("first", false, 10);
CPPUNIT_ASSERT(one.Length() == 5); CPPUNIT_ASSERT(one.Length() == 5);
KPath two("second", false, 20); KPath two("second", false, 20);
@@ -363,12 +347,10 @@ KPathTest::TestAssign()
CPPUNIT_ASSERT(one.Length() == 9); CPPUNIT_ASSERT(one.Length() == 9);
CPPUNIT_ASSERT(one.BufferSize() == two.BufferSize()); CPPUNIT_ASSERT(one.BufferSize() == two.BufferSize());
CPPUNIT_ASSERT(strcmp(one.Path(), "/whatever") == 0); CPPUNIT_ASSERT(strcmp(one.Path(), "/whatever") == 0);
} }
void TestEquals()
void {
KPathTest::TestEquals()
{
KPath a("one"); KPath a("one");
KPath b("two"); KPath b("two");
CPPUNIT_ASSERT_MESSAGE("1.", !(a == b)); CPPUNIT_ASSERT_MESSAGE("1.", !(a == b));
@@ -378,12 +360,10 @@ KPathTest::TestEquals()
b = "ones"; b = "ones";
CPPUNIT_ASSERT_MESSAGE("3.", !(a == b)); CPPUNIT_ASSERT_MESSAGE("3.", !(a == b));
} }
void TestNotEquals()
void {
KPathTest::TestNotEquals()
{
KPath a("one"); KPath a("one");
KPath b("two"); KPath b("two");
CPPUNIT_ASSERT_MESSAGE("1.", a != b); CPPUNIT_ASSERT_MESSAGE("1.", a != b);
@@ -393,38 +373,8 @@ KPathTest::TestNotEquals()
b = "ones"; b = "ones";
CPPUNIT_ASSERT_MESSAGE("3.", a != b); CPPUNIT_ASSERT_MESSAGE("3.", a != b);
} }
};
/*static*/ void CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(KPathTest, getTestSuiteName());
KPathTest::AddTests(BTestSuite& parent)
{
CppUnit::TestSuite& suite = *new CppUnit::TestSuite("KPathTest");
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestSetToAndPath", &KPathTest::TestSetToAndPath));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestLazyAlloc", &KPathTest::TestLazyAlloc));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestLeaf", &KPathTest::TestLeaf));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestReplaceLeaf", &KPathTest::TestReplaceLeaf));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestRemoveLeaf", &KPathTest::TestRemoveLeaf));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestAdopt", &KPathTest::TestAdopt));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestLockBuffer", &KPathTest::TestLockBuffer));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestDetachBuffer", &KPathTest::TestDetachBuffer));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestNormalize", &KPathTest::TestNormalize));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestAssign", &KPathTest::TestAssign));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestEquals", &KPathTest::TestEquals));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestNotEquals", &KPathTest::TestNotEquals));
parent.addTest("KPathTest", &suite);
}
-35
View File
@@ -1,35 +0,0 @@
/*
* Copyright 2017, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef KPATH_TEST_H
#define KPATH_TEST_H
#include <TestCase.h>
#include <TestSuite.h>
class KPathTest : public CppUnit::TestCase {
public:
KPathTest();
virtual ~KPathTest();
void TestSetToAndPath();
void TestLazyAlloc();
void TestLeaf();
void TestReplaceLeaf();
void TestRemoveLeaf();
void TestAdopt();
void TestLockBuffer();
void TestDetachBuffer();
void TestNormalize();
void TestAssign();
void TestEquals();
void TestNotEquals();
static void AddTests(BTestSuite& suite);
};
#endif // KPATH_TEST_H
@@ -4,18 +4,11 @@
*/ */
#include <TestSuite.h>
#include <TestSuiteAddon.h> #include <TestSuiteAddon.h>
#include "KPathTest.h"
const char*
BTestSuite* getTestSuiteName()
getTestSuite()
{ {
BTestSuite* suite = new BTestSuite("KernelFS"); return "KernelFS";
KPathTest::AddTests(*suite);
return suite;
} }