xref: /haiku/src/tests/kits/storage/BasicTest.h (revision 39241fe22890fb958b6ba32d6ab9526da98be187)
1 // BasicTest.h
2 
3 #ifndef __sk_basic_test_h__
4 #define __sk_basic_test_h__
5 
6 #include <SupportDefs.h>
7 #include <TestCase.h>
8 #include <TestShell.h>
9 #include <set>
10 #include <stdio.h>
11 
12 class BasicTest : public BTestCase
13 {
14 public:
15 	BasicTest();
16 
17 	// This function called before *each* test added in Suite()
18 	void setUp();
19 
20 	// This function called after *each* test added in Suite()
21 	void tearDown();
22 
23 	// helper functions
24 
25 //	void nextSubTest();
26 //	void nextSubTestBlock();
27 
28 	static void execCommand(const string &command);
29 
30 	static void dumpStat(struct stat &st);
31 
32 	static void createVolume(string imageFile, string mountPoint, int32 megs,
33 							 bool makeMountPoint = true);
34 	static void deleteVolume(string imageFile, string mountPoint,
35 							 bool deleteMountPoint = true);
36 
37 protected:
38 	int32 fSubTestNumber;
39 	int32 fAvailableFDs;
40 };
41 
42 
43 // Some other helpful stuff.
44 
45 // == for struct stat
46 static
47 inline
48 bool
49 operator==(const struct stat &st1, const struct stat &st2)
50 {
51 	return (
52 		st1.st_dev == st2.st_dev
53 		&& st1.st_ino == st2.st_ino
54 		&& st1.st_mode == st2.st_mode
55 		&& st1.st_nlink == st2.st_nlink
56 		&& st1.st_uid == st2.st_uid
57 		&& st1.st_gid == st2.st_gid
58 		&& st1.st_size == st2.st_size
59 		&& st1.st_blksize == st2.st_blksize
60 		&& st1.st_atime == st2.st_atime
61 		&& st1.st_mtime == st2.st_mtime
62 		&& st1.st_ctime == st2.st_ctime
63 		&& st1.st_crtime == st2.st_crtime
64 	);
65 }
66 
67 // first parameter is equal to the second or third
68 template<typename A, typename B, typename C>
69 static
70 inline
71 bool
72 equals(const A &a, const B &b, const C &c)
73 {
74 	return (a == b || a == c);
75 }
76 
77 // A little helper class for tests. It works like a set of strings, that
78 // are marked tested or untested.
79 class TestSet {
80 public:
81 	typedef set<string> nameset;
82 
83 public:
84 	TestSet()
85 	{
86 	}
87 
88 	void add(string name)
89 	{
90 		if (fTestedNames.find(name) == fTestedNames.end())
91 			fUntestedNames.insert(name);
92 	}
93 
94 	void remove(string name)
95 	{
96 		if (fUntestedNames.find(name) != fUntestedNames.end())
97 			fUntestedNames.erase(name);
98 		else if (fTestedNames.find(name) != fTestedNames.end())
99 			fTestedNames.erase(name);
100 	}
101 
102 	void clear()
103 	{
104 		fUntestedNames.clear();
105 		fTestedNames.clear();
106 	}
107 
108 	void rewind()
109 	{
110 		fUntestedNames.insert(fTestedNames.begin(), fTestedNames.end());
111 		fTestedNames.clear();
112 	}
113 
114 	bool test(string name, bool dump = BTestShell::GlobalBeVerbose())
115 	{
116 		bool result = (fUntestedNames.find(name) != fUntestedNames.end());
117 		if (result) {
118 			fUntestedNames.erase(name);
119 			fTestedNames.insert(name);
120 		} else if (dump) {
121 			// dump untested
122 			printf("TestSet::test(`%s')\n", name.c_str());
123 			printf("untested:\n");
124 			for (nameset::iterator it = fUntestedNames.begin();
125 				 it != fUntestedNames.end();
126 				 ++it) {
127 				printf("  `%s'\n", it->c_str());
128 			}
129 		}
130 		return result;
131 	}
132 
133 	bool testDone()
134 	{
135 		return (fUntestedNames.empty());
136 	}
137 
138 private:
139 	nameset	fUntestedNames;
140 	nameset	fTestedNames;
141 };
142 
143 
144 #endif	// __sk_basic_test_h__
145