kernel32: Renamed the kernel directory to kernel32.
[wine/multimedia.git] / dlls / kernel32 / tests / profile.c
blob4379ee29e17b198f9484359058af13be0586ece7
1 /*
2 * Unit tests for profile functions
4 * Copyright (c) 2003 Stefan Leichter
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "windows.h"
28 #define KEY "ProfileInt"
29 #define SECTION "Test"
30 #define TESTFILE ".\\testwine.ini"
31 #define TESTFILE2 ".\\testwine2.ini"
33 struct _profileInt {
34 LPCSTR section;
35 LPCSTR key;
36 LPCSTR value;
37 LPCSTR iniFile;
38 INT defaultVal;
39 UINT result;
40 UINT result9x;
43 static void test_profile_int(void)
45 struct _profileInt profileInt[]={
46 { NULL, NULL, NULL, NULL, 70, 0 , 0}, /* 0 */
47 { NULL, NULL, NULL, TESTFILE, -1, 4294967295U, 0},
48 { NULL, NULL, NULL, TESTFILE, 1, 1 , 0},
49 { SECTION, NULL, NULL, TESTFILE, -1, 4294967295U, 0},
50 { SECTION, NULL, NULL, TESTFILE, 1, 1 , 0},
51 { NULL, KEY, NULL, TESTFILE, -1, 4294967295U, 0}, /* 5 */
52 { NULL, KEY, NULL, TESTFILE, 1, 1 , 0},
53 { SECTION, KEY, NULL, TESTFILE, -1, 4294967295U, 4294967295U},
54 { SECTION, KEY, NULL, TESTFILE, 1, 1 , 1},
55 { SECTION, KEY, "-1", TESTFILE, -1, 4294967295U, 4294967295U},
56 { SECTION, KEY, "-1", TESTFILE, 1, 4294967295U, 4294967295U}, /* 10 */
57 { SECTION, KEY, "1", TESTFILE, -1, 1 , 1},
58 { SECTION, KEY, "1", TESTFILE, 1, 1 , 1},
59 { SECTION, KEY, "+1", TESTFILE, -1, 1 , 0},
60 { SECTION, KEY, "+1", TESTFILE, 1, 1 , 0},
61 { SECTION, KEY, "4294967296", TESTFILE, -1, 0 , 0}, /* 15 */
62 { SECTION, KEY, "4294967296", TESTFILE, 1, 0 , 0},
63 { SECTION, KEY, "4294967297", TESTFILE, -1, 1 , 1},
64 { SECTION, KEY, "4294967297", TESTFILE, 1, 1 , 1},
65 { SECTION, KEY, "-4294967297", TESTFILE, -1, 4294967295U, 4294967295U},
66 { SECTION, KEY, "-4294967297", TESTFILE, 1, 4294967295U, 4294967295U}, /* 20 */
67 { SECTION, KEY, "42A94967297", TESTFILE, -1, 42 , 42},
68 { SECTION, KEY, "42A94967297", TESTFILE, 1, 42 , 42},
69 { SECTION, KEY, "B4294967297", TESTFILE, -1, 0 , 0},
70 { SECTION, KEY, "B4294967297", TESTFILE, 1, 0 , 0},
72 int i, num_test = (sizeof(profileInt)/sizeof(struct _profileInt));
73 UINT res;
75 DeleteFileA( TESTFILE);
77 for (i=0; i < num_test; i++) {
78 if (profileInt[i].value)
79 WritePrivateProfileStringA(SECTION, KEY, profileInt[i].value,
80 profileInt[i].iniFile);
82 res = GetPrivateProfileIntA(profileInt[i].section, profileInt[i].key,
83 profileInt[i].defaultVal, profileInt[i].iniFile);
84 ok((res == profileInt[i].result) || (res == profileInt[i].result9x),
85 "test<%02d>: ret<%010u> exp<%010u><%010u>\n",
86 i, res, profileInt[i].result, profileInt[i].result9x);
89 DeleteFileA( TESTFILE);
92 static void test_profile_string(void)
94 HANDLE h;
95 int ret;
96 DWORD count;
97 char buf[100];
98 char *p;
99 /* test that lines without an '=' will not be enumerated */
100 /* in the case below, name2 is a key while name3 is not. */
101 char content[]="[s]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n";
102 DeleteFileA( TESTFILE2);
103 h = CreateFileA( TESTFILE2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
104 FILE_ATTRIBUTE_NORMAL, NULL);
105 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", TESTFILE2);
106 if( h == INVALID_HANDLE_VALUE) return;
107 WriteFile( h, content, sizeof(content), &count, NULL);
108 CloseHandle( h);
110 /* enumerate the keys */
111 ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
112 TESTFILE2);
113 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
114 p[-1] = ',';
115 /* and test */
116 ok( ret == 18 && !strcmp( buf, "name1,name2,name4"), "wrong keys returned(%d): %s\n", ret,
117 buf);
119 ret=GetPrivateProfileSectionA("s", buf, sizeof(buf), TESTFILE2);
120 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
121 p[-1] = ',';
122 /* and test */
123 ok( ret == 35 && !strcmp( buf, "name1=val1,name2=,name3,name4=val4"), "wrong section returned(%d): %s\n",
124 ret, buf);
126 /* add a new key to test that the file is quite usable */
127 WritePrivateProfileStringA( "s", "name5", "val5", TESTFILE2);
128 ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
129 TESTFILE2);
130 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
131 p[-1] = ',';
132 ok( ret == 24 && !strcmp( buf, "name1,name2,name4,name5"), "wrong keys returned(%d): %s\n",
133 ret, buf);
135 DeleteFileA( TESTFILE2);
138 static void test_profile_sections(void)
140 HANDLE h;
141 int ret;
142 DWORD count;
143 char buf[100];
144 WCHAR bufW[100];
145 static const char content[]="[section1]\r\n[section2]\r\n[section3]\r\n";
146 static const char testfile3[]=".\\testwine3.ini";
147 static const WCHAR testfile3W[]={ '.','\\','t','e','s','t','w','i','n','e','3','.','i','n','i',0 };
148 DeleteFileA( testfile3 );
149 h = CreateFileA( testfile3, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
150 FILE_ATTRIBUTE_NORMAL, NULL);
151 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile3);
152 if( h == INVALID_HANDLE_VALUE) return;
153 WriteFile( h, content, sizeof(content), &count, NULL);
154 CloseHandle( h);
156 /* Test with sufficiently large buffer */
157 ret = GetPrivateProfileSectionNamesA( buf, 29, testfile3 );
158 ok( ret == 27, "expected return size 27, got %d\n", ret );
159 ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
161 /* Test with exactly fitting buffer */
162 ret = GetPrivateProfileSectionNamesA( buf, 28, testfile3 );
163 ok( ret == 26, "expected return size 26, got %d\n", ret );
164 ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
166 /* Test with a buffer too small */
167 ret = GetPrivateProfileSectionNamesA( buf, 27, testfile3 );
168 ok( ret == 25, "expected return size 25, got %d\n", ret );
169 ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
172 /* Test with sufficiently large buffer */
173 ret = GetPrivateProfileSectionNamesW( bufW, 29, testfile3W );
174 ok( ret == 27, "expected return size 27, got %d\n", ret );
175 ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
177 /* Test with exactly fitting buffer */
178 ret = GetPrivateProfileSectionNamesW( bufW, 28, testfile3W );
179 ok( ret == 26, "expected return size 26, got %d\n", ret );
180 ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
182 /* Test with a buffer too small */
183 ret = GetPrivateProfileSectionNamesW( bufW, 27, testfile3W );
184 ok( ret == 25, "expected return size 25, got %d\n", ret );
185 ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
187 DeleteFileA( testfile3 );
190 START_TEST(profile)
192 test_profile_int();
193 test_profile_string();
194 test_profile_sections();