-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_directories.c
More file actions
90 lines (78 loc) · 3 KB
/
test_directories.c
File metadata and controls
90 lines (78 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Test for directory operations (CreateDirectoryW, RemoveDirectoryW)
*/
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
int main() {
printf("Testing Directory Operations...\n\n");
// Test 1: CreateDirectoryW
printf("Test 1: CreateDirectoryW\n");
if (!CreateDirectoryW(L"C:\\temp\\test_dir", NULL)) {
printf(" ❌ CreateDirectoryW failed!\n");
return 1;
}
printf(" ✅ CreateDirectoryW succeeded\n");
// Test 2: Create nested directory
printf("\nTest 2: Create nested directory\n");
if (!CreateDirectoryW(L"C:\\temp\\test_dir\\subdir", NULL)) {
printf(" ❌ CreateDirectoryW (nested) failed!\n");
return 1;
}
printf(" ✅ Nested directory created\n");
// Test 3: Try to create existing directory (should fail)
printf("\nTest 3: Try to create existing directory\n");
if (CreateDirectoryW(L"C:\\temp\\test_dir", NULL)) {
printf(" ❌ Should have failed on existing directory!\n");
return 1;
}
printf(" ✅ Correctly failed on existing directory\n");
// Test 4: Create a file in the directory to test cleanup
printf("\nTest 4: Create file in directory\n");
HANDLE hFile = CreateFileW(L"C:\\temp\\test_dir\\test.txt",
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf(" ❌ CreateFileW failed!\n");
return 1;
}
const char* data = "Test file in directory\n";
DWORD bytesWritten;
WriteFile(hFile, data, strlen(data), &bytesWritten, NULL);
CloseHandle(hFile);
printf(" ✅ File created in directory\n");
// Test 5: Try to remove non-empty directory (should fail)
printf("\nTest 5: Try to remove non-empty directory\n");
if (RemoveDirectoryW(L"C:\\temp\\test_dir")) {
printf(" ❌ Should have failed on non-empty directory!\n");
return 1;
}
printf(" ✅ Correctly failed on non-empty directory\n");
// Test 6: Clean up - delete file first
printf("\nTest 6: Delete file\n");
if (!DeleteFileW(L"C:\\temp\\test_dir\\test.txt")) {
printf(" ❌ DeleteFileW failed!\n");
return 1;
}
printf(" ✅ File deleted\n");
// Test 7: Remove subdirectory
printf("\nTest 7: Remove subdirectory\n");
if (!RemoveDirectoryW(L"C:\\temp\\test_dir\\subdir")) {
printf(" ❌ RemoveDirectoryW (subdir) failed!\n");
return 1;
}
printf(" ✅ Subdirectory removed\n");
// Test 8: Remove main directory (now empty)
printf("\nTest 8: Remove main directory\n");
if (!RemoveDirectoryW(L"C:\\temp\\test_dir")) {
printf(" ❌ RemoveDirectoryW failed!\n");
return 1;
}
printf(" ✅ Main directory removed\n");
printf("\n🎉 All tests passed!\n");
return 0;
}