<commit_before>#define _GNU_SOURCE /* asprintf */
#include <string.h>

#include "support.h"

#define CSYNC_TEST 1
#include "csync_journal.c"

CSYNC *csync;
const char *testdb = (char *) "/tmp/check_csync1/test.db";
const char *testtmpdb = (char *) "/tmp/check_csync1/test.db.ctmp";

static void setup(void) {
  fail_if(system("rm -rf /tmp/check_csync1") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync1") < 0, "Setup failed");
  fail_if(csync_create(&csync, "/tmp/check_csync1", "/tmp/check_csync2") < 0, "Setup failed");
  SAFE_FREE(csync->options.config_dir);
  csync->options.config_dir = c_strdup("/tmp/check_csync1/");
}

static void teardown(void) {
  fail_if(csync_destroy(csync) < 0, "Teardown failed");
  fail_if(system("rm -rf /tmp/check_csync1") < 0, "Teardown failed");
}

START_TEST (check_csync_journal_check)
{
  fail_if(system("mkdir -p /tmp/check_csync1") < 0, NULL);

  /* old db */
  fail_if(system("echo \"SQLite format 2\" > /tmp/check_csync1/test.db") < 0, NULL);
  fail_unless(csync_journal_check(testdb) == 0);

  /* db already exists */
  fail_unless(csync_journal_check(testdb) == 0);

  /* no db exists */
  fail_if(system("rm -f /tmp/check_csync1/test.db") < 0, NULL);
  fail_unless(csync_journal_check(testdb) == 0);

  fail_unless(csync_journal_check((char *) "/tmp/check_csync1/") < 0);

  fail_if(system("rm -rf /tmp/check_csync1") < 0, NULL);
}
END_TEST

START_TEST (check_csync_journal_load)
{
  struct stat sb;
  fail_unless(csync_journal_load(csync, testdb) == 0, NULL);

  fail_unless(lstat(testtmpdb, &sb) == 0, NULL);

  sqlite3_close(csync->journal.db);
}
END_TEST

START_TEST (check_csync_journal_close)
{
  struct stat sb;
  /* journal not written */
  csync_journal_load(csync, testdb);

  fail_unless(csync_journal_close(csync, testdb, 0) == 0, NULL);

  fail_unless(lstat(testtmpdb, &sb) == 0, NULL);

  csync_journal_load(csync, testdb);

  /* journal written */
  fail_unless(csync_journal_close(csync, testdb, 1) == 0, NULL);

  fail_unless(lstat(testtmpdb, &sb) < 0, NULL);
}
END_TEST

static Suite *make_csync_suite(void) {
  Suite *s = suite_create("csync_journal");

  create_case(s, "check_csync_journal_check", check_csync_journal_check);
  create_case_fixture(s, "check_csync_journal_load", check_csync_journal_load, setup, teardown);
  create_case_fixture(s, "check_csync_journal_close", check_csync_journal_close, setup, teardown);

  return s;
}

int main(int argc, char **argv) {
  Suite *s = NULL;
  SRunner *sr = NULL;
  struct argument_s arguments;
  int nf;
<commit_msg>Improve testcase for journal close function.<commit_after>#define _GNU_SOURCE /* asprintf */
#include <string.h>
#include <unistd.h>

#include "support.h"

#define CSYNC_TEST 1
#include "csync_journal.c"

CSYNC *csync;
const char *testdb = (char *) "/tmp/check_csync1/test.db";
const char *testtmpdb = (char *) "/tmp/check_csync1/test.db.ctmp";

static void setup(void) {
  fail_if(system("rm -rf /tmp/check_csync1") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync1") < 0, "Setup failed");
  fail_if(csync_create(&csync, "/tmp/check_csync1", "/tmp/check_csync2") < 0, "Setup failed");
  SAFE_FREE(csync->options.config_dir);
  csync->options.config_dir = c_strdup("/tmp/check_csync1/");
}

static void teardown(void) {
  fail_if(csync_destroy(csync) < 0, "Teardown failed");
  fail_if(system("rm -rf /tmp/check_csync1") < 0, "Teardown failed");
}

START_TEST (check_csync_journal_check)
{
  fail_if(system("mkdir -p /tmp/check_csync1") < 0, NULL);

  /* old db */
  fail_if(system("echo \"SQLite format 2\" > /tmp/check_csync1/test.db") < 0, NULL);
  fail_unless(csync_journal_check(testdb) == 0);

  /* db already exists */
  fail_unless(csync_journal_check(testdb) == 0);

  /* no db exists */
  fail_if(system("rm -f /tmp/check_csync1/test.db") < 0, NULL);
  fail_unless(csync_journal_check(testdb) == 0);

  fail_unless(csync_journal_check((char *) "/tmp/check_csync1/") < 0);

  fail_if(system("rm -rf /tmp/check_csync1") < 0, NULL);
}
END_TEST

START_TEST (check_csync_journal_load)
{
  struct stat sb;
  fail_unless(csync_journal_load(csync, testdb) == 0, NULL);

  fail_unless(lstat(testtmpdb, &sb) == 0, NULL);

  sqlite3_close(csync->journal.db);
}
END_TEST

START_TEST (check_csync_journal_close)
{
  struct stat sb;
  time_t modtime;
  /* journal not written */
  csync_journal_load(csync, testdb);

  fail_unless(lstat(testdb, &sb) == 0, NULL);
  modtime = sb.st_mtime;

  fail_unless(csync_journal_close(csync, testdb, 0) == 0, NULL);

  fail_unless(lstat(testdb, &sb) == 0, NULL);
  fail_unless(modtime == sb.st_mtime, NULL);

  csync_journal_load(csync, testdb);

  fail_unless(lstat(testdb, &sb) == 0, NULL);
  modtime = sb.st_mtime;

  /* wait a sec or the modtime will be the same */
  sleep(1);

  /* journal written */
  fail_unless(csync_journal_close(csync, testdb, 1) == 0, NULL);

  fail_unless(lstat(testdb, &sb) == 0, NULL);
  fail_unless(modtime < sb.st_mtime, NULL);
}
END_TEST

static Suite *make_csync_suite(void) {
  Suite *s = suite_create("csync_journal");

  create_case(s, "check_csync_journal_check", check_csync_journal_check);
  create_case_fixture(s, "check_csync_journal_load", check_csync_journal_load, setup, teardown);
  create_case_fixture(s, "check_csync_journal_close", check_csync_journal_close, setup, teardown);

  return s;
}

int main(int argc, char **argv) {
  Suite *s = NULL;
  SRunner *sr = NULL;
  struct argument_s arguments;
  int nf;

{'fraction_non_alphanumeric': 0.15394471901885412, 'fraction_numerical': 0.014094819696137652, 'mean_word_length': 5.071111111111111, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5649471', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2150, 'n_words': 414}