END_TEST START_TEST(test_string_replchar) { char *mystring; mystring = (char *)calloc(1024, sizeof(char)); fail_unless(NULL != mystring); // NULL should be handled without error string_replchar(NULL, 'a', 'b'); // string empty string_replchar(mystring, 'a', 'b'); fail_unless('\0' == *mystring); // simple replacement snprintf(mystring, 1024, "abcdefga"); string_replchar(mystring, 'a', 'z'); fail_unless(strcmp("zbcdefgz", mystring) == 0); // no replacement string_replchar(mystring, 'a', 'z'); fail_unless(strcmp("zbcdefgz", mystring) == 0); }
/***************************************************************************** * Convert size into human readable format (e.g. 100,256Kb or 20.7Mb) * * Arguments: * buffer pointer to the buffer for resulting string * size size in bytes that you want to print * * Return value: * Pointer to the string */ char *string_humansize(char *buffer, size_t size) { if( size < 1024 ) sprintf(buffer, "%ldb", (long)size); else if( size < 100*1024 ) sprintf(buffer, "%.1fK", ((double)size)/1024.0); else if( size < 1000*1024 ) sprintf(buffer, "%ldK", (long)(size/1024)); else if( size < 100*1024*1024 ) sprintf(buffer, "%.1fM", ((double)size)/(1024.0*1024.0)); else if( size < 1000*1024*1024 ) sprintf(buffer, "%ldM", (long)(size/(1024*1024))); else sprintf(buffer, "%.1fG", ((double)size)/(1024.0*1024.0*1024.0)); return string_replchar(buffer, ',', '.'); }