Example #1
0
END_TEST

START_TEST(test_str_split_path)
{
	str_t *str = str_from_cstr("test/path");
	str_t *half1, *half2;
	half1 = str_split_path(str, &half2);
	CHECK_STR(half1, >= 4, == 4, "test");
	CHECK_STR(half2, >= 4, == 4, "path");
	str_free(str);
	str_free(half1);
	str_free(half2);

	str = str_from_cstr("test/my/path");
	half1 = str_split_path(str, &half2);
	CHECK_STR(half1, >= 7, == 7, "test/my");
	CHECK_STR(half2, >= 4, == 4, "path");
	str_free(str);
	str_free(half1);
	str_free(half2);

	str = str_from_cstr("testpath");
	half1 = str_split_path(str, &half2);
	fail_unless(half1 == 0, "zero value expected");
	CHECK_STR(half2, >= 8, == 8, "testpath");
	str_free(str);
	str_free(half2);
}
Example #2
0
END_TEST

START_TEST(test_fstr_add_str)
{
	char buf[11];
	fstr_t fstr;

	FSTR_INIT_FOR_BUF(&fstr, buf);
	str_t *str = str_from_cstr("hello");

	fstr_add_str(&fstr, str);
	CHECK_STR(&fstr, == 10, == 5, "hello");
	str_free(str);

	str = str_from_cstr("world!");
	fstr_add_str(&fstr, str);
	CHECK_STR(&fstr, == 10, == 10, "helloworld");
	str_free(str);

	FSTR_INIT_FOR_BUF(&fstr, buf);
	str = str_from_cstr("");
	fstr_add_str(&fstr, str);
	CHECK_STR(&fstr, == 10, == 0, "");
	str_free(str);
}
Example #3
0
END_TEST

START_TEST(test_str_add_str)
{
	str_t *str1 = str_from_cstr("123");
	str_t *str2 = str_from_cstr("456");
	str_add_str(&str1, str2);
	CHECK_STR(str1, >= 6, == 6, "123456");
	str_free(str1);
	str_free(str2);

	str1 = str_new(0);
	str2 = str_from_cstr("preved");
	str_add_str(&str1, str2);
	CHECK_STR(str1, >= 6, == 6, "preved");
	str_free(str2);

	str2 = str_from_cstr("medved");
	str_add_str(&str1, str2);
	CHECK_STR(str1, >= 12, == 12, "prevedmedved");
	str_free(str2);

	str2 = str_from_cstr("");
	str_add_str(&str1, str2);
	CHECK_STR(str1, >= 12, == 12, "prevedmedved");
	str_free(str2);

	str_free(str1);
}
Example #4
0
END_TEST

START_TEST(test_str_from_cstr)
{
	// simple string
	str_t *str = str_from_cstr("Hello, nsf");
	CHECK_STR(str,
		  == strlen("Hello, nsf"),
		  == strlen("Hello, nsf"),
		  "Hello, nsf");
	str_free(str);

	// empty string
	str = str_from_cstr("");
	CHECK_STR(str, == STR_DEFAULT_CAPACITY, == 0, "");
	str_free(str);
}
Example #5
0
static void change_dir(const char *filename)
{
	str_t *dir, *fn;
	fn = str_from_cstr(filename);
	dir = str_split_path(fn, 0);
	chdir(dir->data);
	str_free(dir);
	str_free(fn);
}
Example #6
0
END_TEST

START_TEST(test_str_clear)
{
	str_t *str = str_from_cstr("nsf");
	str_clear(str);
	CHECK_STR(str, >= 3, == 0, "");
	str_free(str);
}
Example #7
0
END_TEST

START_TEST(test_str_dup)
{
	str_t *str1 = str_from_cstr("hello123");
	str_t *str2 = str_dup(str1);
	CHECK_STR(str2, == 8, == 8, "hello123");
	str_free(str1);
	str_free(str2);
}
Example #8
0
END_TEST

START_TEST(test_str_trim)
{
	str_t *str = str_from_cstr(" 	\n\r123  \n");
	str_trim(str);
	CHECK_STR(str, >= 3, == 3, "123");
	str_free(str);

	str = str_new(0);
	str_trim(str);
	CHECK_STR(str, == STR_DEFAULT_CAPACITY, == 0, "");
	str_free(str);
}
Example #9
0
END_TEST

START_TEST(test_str_ensure_cap)
{
	// zero capacity
	str_t *str = str_new(0);
	str_ensure_cap(&str, 10);
	CHECK_STR(str, >= 10, == 0, "");
	str_free(str);

	// non-zero length and capacity
	str = str_from_cstr("hello");
	str_ensure_cap(&str, 1);
	CHECK_STR(str, >= 6, == 5, "hello");
	str_free(str);

	// rare case, requested capacity is more than 2x bigger than the
	// previous capacity, just for the sake of coverage
	str = str_from_cstr("hello");
	str_ensure_cap(&str, 50);
	CHECK_STR(str, >= 50, == 5, "hello");
	str_free(str);
}
Example #10
0
END_TEST

START_TEST(test_str_add_printf)
{
	str_t *str = str_from_cstr("123");
	str_add_printf(&str, "%d", 456);
	CHECK_STR(str, >= 6, == 6, "123456");
	str_free(str);

	str = str_new(0);
	str_add_printf(&str, "%d", 31337);
	str_add_printf(&str, "%s", "31337");
	CHECK_STR(str, >= 10, == 10, "3133731337");
	str_free(str);
}
Example #11
0
END_TEST

START_TEST(test_str_add_file)
{
	str_t *str = str_from_cstr("abc");
	str_add_file(&str, "testdata/file.txt");
	CHECK_STR(str, >= 13, == 13, "abc123456789\n");
	str_free(str);

	str = str_new(0);
	str_add_file(&str, "testdata/file.txt");
	CHECK_STR(str, >= 10, == 10, "123456789\n");
	str_free(str);

	str = str_new(0);
	str_add_file(&str, "non-existent file");
	CHECK_STR(str, == STR_DEFAULT_CAPACITY, == 0, "");
	str_free(str);
}
Example #12
0
END_TEST

START_TEST(test_str_add_cstr_len)
{
	str_t *str = str_from_cstr("123");
	str_add_cstr_len(&str, "456", 3);
	CHECK_STR(str, >= 6, == 6, "123456");
	str_free(str);

	str = str_new(0);
	str_add_cstr_len(&str, "12345", 0);
	str_add_cstr_len(&str, "12345", 1);
	str_add_cstr_len(&str, "12345", 2);
	str_add_cstr_len(&str, "12345", 3);
	str_add_cstr_len(&str, "12345", 4);
	str_add_cstr_len(&str, "12345", 5);
	str_add_cstr_len(&str, "12345", 0);
	CHECK_STR(str, >= 15, == 15, "112123123412345");
	str_free(str);
}
Example #13
0
int main(void) {
  size_t score = 0;
  
  for (size_t i = 0; i < 500000; ++i) {
    Str s = str_from_cstr("hello, world!");
    str_push_char(&s, 'a');
    str_push_cstr(&s, "xxx");
    str_set_char(&s, '?', 0);
    if (str_char_at(&s, 1) == '?') {
      score += 1;
    }
    score += str_len(&s);
    score += strlen(str_chars(&s));
    str_free(&s);
  }
  
  printf("%ld\n", score);
  
  return 0;
}
Example #14
0
static char *prepend_cwd(const char *file)
{
	str_t *tmp;
	char cwd[1024];
	char *ret;
	char *pcwd;

	pcwd = getcwd(cwd, 1024);
	if (!pcwd) {
		fprintf(stderr, "Path is too long, more than 1024? wtf, man?\n");
		exit(1);
	}

	tmp = str_from_cstr(pcwd);
	str_add_cstr(&tmp, "/");
	str_add_cstr(&tmp, file);
	ret = strdup(tmp->data);
	str_free(tmp);
	return ret;
}