Plan 9 from Bell Labs’s /usr/web/sources/contrib/de0u/root/sys/src/cmd/divergefs/tests/testarray.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 "common.h"
#include "collection.h"
#include "function.h"
#include "array.h"
#include "unittest.h"

typedef struct Range
{
  int min;
  int max;
  int current;
} Range;

typedef struct TestArray
{
  Range range;
  Array *array;
  Array *stack;
} TestArray;

static void *testarray_setup(void)
{
  int i;
  TestArray *data = (TestArray *)emalloc_fs(sizeof(*data));
  data->range.min = 0;
  data->range.max = 20;
  data->range.current = 0;

  data->array = array_new();
  for(i = data->range.min; i <= data->range.max; ++i)
  {
    array_add(data->array, (void *)i);
  }

  data->stack = array_new();
  for(i = data->range.min; i <= data->range.max; ++i)
  {
    array_push(data->stack, (void *)i);
  }

  return data;
}

static void testarray_teardown(void *data)
{
  TestArray *testarray = (TestArray *)data;
  assert_valid(testarray);
  array_free(testarray->array);
  array_free(testarray->stack);
  free(data);
}

static collection_do_ret testarray_add_each(void *p, void *arg)
{
  int i = (int)p;
  Range *range = (Range *)arg;
  if(i != range->current || i < range->min || i > range->max)
  {
    range->current = -1;
    return COLLECTION_DO_STOP;
  }

  ++(range->current);
  return COLLECTION_DO_CONTINUE;
}

static bool testarray_add(void *data)
{
  int i;
  TestArray *testarray = (TestArray *)data;
  Array *array = testarray->array;
  Range *range = &(testarray->range);
  assert_valid(array);
  assert_valid(range);

  test_assert(array_size(array) == (range->max - range->min + 1));

  array_do(array, testarray_add_each, range);
  test_assert_false(range->current == -1);

  for(i = 0; i <= (range->max - range->min); ++i)
  {
    test_assert(((int)array_at(array, i)) == (i + range->min));
  }

  return true;
}

static bool integer_identity(void *p, void *arg)
{
  return ((int)p) == ((int)arg);
}

static bool testarray_find(void *data)
{
  int i;
  int result;
  TestArray *testarray = (TestArray *)data;
  Array *array = testarray->array;
  Range *range = &(testarray->range);
  assert_valid(array);
  assert_valid(range);

  for(i = range->max; i >= range->min; --i)
  {
    test_assert(array_detect(array, 
      integer_identity, (void **)&result, (void *)i));
    test_assert(array_detect(array, 
      integer_identity, nil, (void *)i));
    test_assert(result == i);
  }

  for(i = range->max + 1; i < range->max + 20; ++i)
  {
    test_assert_false(array_detect(array, 
      integer_identity, (void **)&result, (void *)i));
    test_assert_false(array_detect(array, 
      integer_identity, nil, (void *)i));
    test_assert(((void *)result) == nil);
  }

  return true;
}

static bool testarray_clear(void *data)
{
  TestArray *testarray = (TestArray *)data;
  Array *array = testarray->array;
  assert_valid(array);

  array_clear(array);
  test_assert(array_size(array) == 0);
  return true;
}

static bool testarray_stack(void *data)
{
  int current;
  Array *stack;
  TestArray *testarray = (TestArray *)data;
  assert_valid(testarray);
  stack = testarray->stack;
  assert_valid(stack);

  for(current = testarray->range.max; array_notempty(stack); --current)
  {
    test_assert((int)array_pop(testarray->stack) == current);
  }
  return true;
}

bool testarray_resize(void *data)
{
  int i;
  TestArray *testarray = (TestArray *)data;
  Array *array = testarray->array;
  Range *range = &(testarray->range);

  array_resize(array, 5);
  test_assert(array_size(array) == 5);
  for(i = 0; i < 5; ++i)
  {
    test_assert((int)array_at(array, i) == (i + range->min));
  }

  return true;
}

bool testarray_put(void *data)
{
  TestArray *testarray = (TestArray *)data;
  Array *array = testarray->array;
  
  array_put(array, 0, (void *)3);
  test_assert((int)array_at(array, 0) == 3);
  return true;
}

static TestFixure testarray_fixure =
{
  .setup    = testarray_setup,
  .teardown = testarray_teardown
};

static TestCaseNamePair testarray_testcases[] =
{
  testcasenamepair_make(testarray_add),
  testcasenamepair_make(testarray_find),
  testcasenamepair_make(testarray_clear),
  testcasenamepair_make(testarray_resize),
  testcasenamepair_make(testarray_put),
  testcasenamepair_make(testarray_stack)
};

AbstractTest *testarray_testsuite(void)
{
  return (AbstractTest *)
    testsuite_make("testarray", 
      testarray_testcases, static_array_length(testarray_testcases), 
      &testarray_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 [email protected].