Plan 9 from Bell Labs’s /usr/web/sources/contrib/de0u/root/sys/src/cmd/divergefs/tests/testfilepath.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include <String.h>
#include "common.h"
#include "debug.h"
#include "filepath.h"
#include "unittest.h"

typedef struct TestFilePath
{
  char currentdir[FILEPATH_BUFFER_MAX];
  String *relative;
  String *absolute;
} TestFilePath;

static void *testfilepath_setup(void)
{
  TestFilePath *data = (TestFilePath *)emalloc_fs(sizeof(*data));
  getwd(data->currentdir, FILEPATH_BUFFER_MAX);
  data->relative = s_copy("src/tests");
  data->absolute = s_copy("/mnt/term/home");
  return data;
}

static void testfilepath_teardown(void *data)
{
  TestFilePath *testfilepath = (TestFilePath *)data;
  assert_valid(testfilepath);
  s_free(testfilepath->relative);
  s_free(testfilepath->absolute);
}

static bool testfilepath_absolute_path(void *data)
{
  bool equal;
  String *clone;
  TestFilePath *testfilepath = (TestFilePath *)data;
  assert_valid(testfilepath);

  test_assert(filepath_isabsolute(testfilepath->absolute));
  clone = s_clone(testfilepath->absolute);
  filepath_make_absolute(&clone);
  equal = strcmp(s_to_c(clone), s_to_c(testfilepath->absolute)) == 0;
  s_free(clone);
  test_assert(equal);
  return true;
}

static bool testfilepath_relative_path(void *data)
{
  bool equal;
  String *completed;
  String *absolute;
  TestFilePath *testfilepath = (TestFilePath *)data;
  assert_valid(testfilepath);

  test_assert_false(filepath_isabsolute(testfilepath->relative));
  test_assert(filepath_isrelative(testfilepath->relative));

  absolute = s_copy(testfilepath->currentdir);
  filepath_append(&absolute, s_to_c(testfilepath->relative));

  completed = s_clone(testfilepath->relative);
  filepath_make_absolute(&completed);

  equal = strcmp(s_to_c(absolute), s_to_c(completed)) == 0;
  s_free(absolute);
  s_free(completed);

  test_assert(equal);
  return true;
}

static TestFixure testfilepath_fixure =
{
  .setup    = testfilepath_setup,
  .teardown = testfilepath_teardown
};

static TestCaseNamePair testfilepath_testcases[] =
{
  testcasenamepair_make(testfilepath_absolute_path),
  testcasenamepair_make(testfilepath_relative_path)
};

AbstractTest *testfilepath_testsuite(void)
{
  return (AbstractTest *)
    testsuite_make("testfilepath", 
      testfilepath_testcases, static_array_length(testfilepath_testcases), 
      &testfilepath_fixure);
}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.