#include "test_cpath.h"
#include "cpath.h"
#include "tester.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

TestFuncResult test_cpath_join_path(void) {
  char expected[4096] = {0};
  char out[4096]      = {0};
  char tmp[1024]      = {0};

  sprintf(expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
  sprintf(tmp, "%c", PATH_SEP);
  wapp_cpath_join_path(out, tmp, "home", "abdelrahman", "Documents");

  bool result = strcmp(out, expected) == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(out, 0, strlen(out));
  sprintf(expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
  wapp_cpath_join_path(out, "home", "abdelrahman", "Documents");

  result = result && strcmp(out, expected) == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(out, 0, strlen(out));
  memset(tmp, 0, strlen(tmp));
  sprintf(expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
  sprintf(tmp, "%chome", PATH_SEP);
  wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents");

  result = result && strcmp(out, expected) == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(out, 0, strlen(out));
  memset(tmp, 0, strlen(tmp));
  sprintf(expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
  sprintf(tmp, "home%c", PATH_SEP);
  wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents");

  result = result && strcmp(out, expected) == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(out, 0, strlen(out));
  memset(tmp, 0, strlen(tmp));
  sprintf(expected, "%chome", PATH_SEP);
  sprintf(tmp, "%chome", PATH_SEP);
  wapp_cpath_join_path(out, tmp, "");

  result = result && strcmp(out, expected) == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(out, 0, strlen(out));
  sprintf(expected, "");
  wapp_cpath_join_path(out, "", "");

  result = result && strcmp(out, expected) == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(out, 0, strlen(out));
  sprintf(expected, "home");
  wapp_cpath_join_path(out, "", "home");

  result = result && strcmp(out, expected) == 0;

  return wapp_tester_result(result);
}

TestFuncResult test_cpath_dirname(void) {
  char buf[4096] = {0};
  char tmp[4096] = {0};

  wapp_cpath_dirname(buf, "/");
  bool result = strcmp(buf, "/") == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(buf, 0, strlen(buf));
  wapp_cpath_dirname(buf, "home");
  result = strcmp(buf, ".") == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(buf, 0, strlen(buf));
  wapp_cpath_dirname(buf, "");
  result = strcmp(buf, ".") == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(buf, 0, strlen(buf));
  sprintf(tmp, "%chome%ctest", PATH_SEP, PATH_SEP);
  wapp_cpath_dirname(buf, tmp);
  result = strcmp(buf, "/home") == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(buf, 0, strlen(buf));
  memset(tmp, 0, strlen(tmp));
  sprintf(tmp, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP);
  wapp_cpath_dirname(buf, tmp);
  result = strcmp(buf, "/home") == 0;

  return wapp_tester_result(result);
}

TestFuncResult test_cpath_dirup(void) {
  char buf[4096] = {0};
  char tmp[4096] = {0};

  wapp_cpath_dirup(buf, 3, "/");
  bool result = strcmp(buf, "/") == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(buf, 0, strlen(buf));
  sprintf(tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
  wapp_cpath_dirup(buf, 3, tmp);
  result = strcmp(buf, "/") == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(buf, 0, strlen(buf));
  memset(tmp, 0, strlen(tmp));
  sprintf(tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
  wapp_cpath_dirup(buf, 3, tmp);
  result = strcmp(buf, ".") == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(buf, 0, strlen(buf));
  sprintf(tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
  wapp_cpath_dirup(buf, 2, tmp);
  result = strcmp(buf, "/home") == 0;
  if (!result) {
    return wapp_tester_result(result);
  }

  memset(buf, 0, strlen(buf));
  memset(tmp, 0, strlen(tmp));
  sprintf(tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
  wapp_cpath_dirup(buf, 2, tmp);
  result = strcmp(buf, "home") == 0;

  return wapp_tester_result(result);
}