Skip to content

Commit e694c71

Browse files
committed
Add tests for byte sizes & suffixes
1 parent 5ca2eb3 commit e694c71

File tree

3 files changed

+143
-11
lines changed

3 files changed

+143
-11
lines changed

hd-tests.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,144 @@ test__compact_size_with_suffix__is_bytes()
102102
}
103103

104104

105+
int
106+
test__compact_size_with_suffix__is_kilobytes()
107+
{
108+
/* Arrange */
109+
int size = 65536;
110+
char result[16] = { 0 };
111+
const char * expected = "64 KB";
112+
113+
/* Act */
114+
compact_size_with_suffix(size, result);
115+
116+
/* Assert */
117+
if (strcmp(result, expected) != 0) {
118+
printf("\nRESULT: %s\nEXPECTED: %s\n", result, expected);
119+
return 1;
120+
}
121+
122+
return 0;
123+
}
124+
125+
126+
int
127+
test__compact_size_with_suffix__is_megabytes()
128+
{
129+
/* Arrange */
130+
int size = 1048576;
131+
char result[16] = { 0 };
132+
const char * expected = "1 MB";
133+
134+
/* Act */
135+
compact_size_with_suffix(size, result);
136+
137+
/* Assert */
138+
if (strcmp(result, expected) != 0) {
139+
printf("\nRESULT: %s\nEXPECTED: %s\n", result, expected);
140+
return 1;
141+
}
142+
143+
return 0;
144+
}
145+
146+
147+
int
148+
test__compact_size_with_suffix__is_not_megabytes()
149+
{
150+
/* Arrange */
151+
int size = 1048575;
152+
char result[16] = { 0 };
153+
const char * expected = "1023 KB";
154+
155+
/* Act */
156+
compact_size_with_suffix(size, result);
157+
158+
/* Assert */
159+
if (strcmp(result, expected) != 0) {
160+
printf("\nRESULT: %s\nEXPECTED: %s\n", result, expected);
161+
return 1;
162+
}
163+
164+
return 0;
165+
}
166+
167+
168+
int
169+
test__compact_size_with_suffix__is_gigabytes()
170+
{
171+
/* Arrange */
172+
long long size = 1073741824;
173+
char result[16] = { 0 };
174+
const char * expected = "1 GB";
175+
176+
/* Act */
177+
compact_size_with_suffix(size, result);
178+
179+
/* Assert */
180+
if (strcmp(result, expected) != 0) {
181+
printf("\nRESULT: %s\nEXPECTED: %s\n", result, expected);
182+
return 1;
183+
}
184+
185+
return 0;
186+
}
187+
188+
189+
int
190+
test__compact_size_with_suffix__is_not_gigabytes()
191+
{
192+
/* Arrange */
193+
long long size = 1073741823;
194+
char result[16] = { 0 };
195+
const char * expected = "1023 MB";
196+
197+
/* Act */
198+
compact_size_with_suffix(size, result);
199+
200+
/* Assert */
201+
if (strcmp(result, expected) != 0) {
202+
printf("\nRESULT: %s\nEXPECTED: %s\n", result, expected);
203+
return 1;
204+
}
205+
206+
return 0;
207+
}
208+
209+
210+
//int
211+
//test__compact_size_with_suffix__works_with_ntfs_volume_limit()
212+
//{
213+
// /* Arrange */
214+
// long long size = 281474976710656;
215+
// char result[16] = { 0 };
216+
// const char * expected = "1023 MB";
217+
//
218+
// /* Act */
219+
// compact_size_with_suffix(size, result);
220+
//
221+
// /* Assert */
222+
// if (strcmp(result, expected) != 0) {
223+
// printf("\nRESULT: %s\nEXPECTED: %s\n", result, expected);
224+
// return 1;
225+
// }
226+
//
227+
// return 0;
228+
//}
229+
230+
105231
int
106232
main()
107233
{
108234
int failed_count = 0;
109235

110236
failed_count += test__compact_size_with_suffix__is_bytes();
237+
failed_count += test__compact_size_with_suffix__is_kilobytes();
238+
failed_count += test__compact_size_with_suffix__is_megabytes();
239+
failed_count += test__compact_size_with_suffix__is_not_megabytes();
240+
failed_count += test__compact_size_with_suffix__is_gigabytes();
241+
failed_count += test__compact_size_with_suffix__is_not_gigabytes();
242+
//failed_count += test__compact_size_with_suffix__works_with_ntfs_volume_limit();
111243
failed_count += test__create_horizontal_line__is_console_width();
112244
//failed_count += test__create_footer();
113245
failed_count += test__get_console_info__sets_attributes();

hd.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ create_horizontal_line(char * result, CONSOLE_SCREEN_BUFFER_INFO csbi)
7373

7474

7575
char *
76-
compact_size_with_suffix(int size_bytes, char * suffixed_size)
76+
compact_size_with_suffix(long long size_bytes, char * suffixed_size)
7777
{
78-
if(size_bytes > 1023) /* KB */
79-
if((size_bytes /= 1024) > 1023) /* MB */
80-
if((size_bytes /= 1024) > 1023) /* GB */
81-
if((size_bytes /= 1024) > 1023) /* TB */
82-
sprintf(suffixed_size, "%d TB", size_bytes);
83-
else sprintf(suffixed_size, "%d GB", size_bytes);
84-
else sprintf(suffixed_size, "%d MB", size_bytes);
85-
else sprintf(suffixed_size, "%d KB", size_bytes);
86-
else sprintf(suffixed_size, "%d B", size_bytes);
78+
if(size_bytes > 1023LL) /* KB */
79+
if((size_bytes /= 1024LL) > 1023LL) /* MB */
80+
if((size_bytes /= 1024LL) > 1023LL) /* GB */
81+
if((size_bytes /= 1024LL) > 1023LL) /* TB */
82+
sprintf(suffixed_size, "%lld TB", size_bytes);
83+
else sprintf(suffixed_size, "%lld GB", size_bytes);
84+
else sprintf(suffixed_size, "%lld MB", size_bytes);
85+
else sprintf(suffixed_size, "%lld KB", size_bytes);
86+
else sprintf(suffixed_size, "%lld B", size_bytes);
8787

8888
return suffixed_size;
8989
}

hd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct console_info {
4545
int build_initial_search_string(char * search_path, char * search_string);
4646
//create_footer(char *footer_string, short console_width, char *root_path, char search_drive);
4747
char * create_horizontal_line(char *, CONSOLE_SCREEN_BUFFER_INFO);
48-
char * compact_size_with_suffix(int, char *);
48+
char * compact_size_with_suffix(long long, char *);
4949
int display_footer();
5050
int display_header(char * search_path);
5151
int display_help();

0 commit comments

Comments
 (0)