Пример #1
0
END_TEST

START_TEST(test_upper_power_of_two)
{
  /* check function */
  static size_t test_pairs[][2] = 
    {
      { 0, 2 },
      { 1, 2 },
      { 7, 8 },
      { 100, 128 },
      { 1023, 1024 },
      { 1024, 1024 },
      { 1025, 2048 },
      { 32 * 1024, 32 * 1024 },
      { (32 * 1024) - 1, 32 * 1024 },
      { (32 * 1024) + 1, 64 * 1024 },
      { 64 * 1024, 64 * 1024 },
      { (64 * 1024) - 1, 64 * 1024 },
      { (64 * 1024) + 1, 128 * 1024 },
      { 0, 2 },
      { -1, -1 }
    };

  int i = 0;
  while ( test_pairs[i][0] != -1 ) {
    size_t testval  = test_pairs[i][0];
    size_t expected = test_pairs[i][1];
    size_t actual   = upper_power_of_two(testval);

    fail_unless( actual == expected );
    i++;
  }

  /* TODO: test overflow behaviour */

  /* checks that BUFFER_MAX is defined as a power of 2 */
  fail_unless(upper_power_of_two(BUFFER_MAX) == BUFFER_MAX);
}
Пример #2
0
static inline void pool_expand(size_t len)
{
  list *newlist = NULL;
  /* Check if we have enough memory already */
  if (memory_pools->size - memory_pools->used >= len) {
    return;
  }
  newlist = (list*) malloc(sizeof(list));
  newlist->next = memory_pools;
  memory_pools = newlist;
  memory_pools->used = 0;
  memory_pools->size = upper_power_of_two(3*memory_pools->next->size/2 + len); /* expand by 1.5x the old memory pool. More if we request a very large array. */
  memory_pools->memory = malloc(memory_pools->size);
}
/*
	Pairwise max - computes the pair maximums using recursive algorithm and by building binary tree
	*arr - array pointer
	startindex - starting index of the array
	endindex - ending index of the array
	return - root node
*/
node* pairmax(int *arr, int startIndex, int endIndex)
{
	if(endIndex-startIndex == 1)
	{
		node *left = newNode(arr[startIndex],NULL,NULL);
		node *right = newNode(arr[endIndex],NULL,NULL);
		node *max = newNode(maximum(arr[startIndex], arr[endIndex]),left,right);
		return max;
	}
	else if (endIndex-startIndex == 0)
	{
		node *right = newNode(arr[endIndex],NULL,NULL);
		return right;
	}
	int m = upper_power_of_two((endIndex-startIndex)+1);
	if( m == (endIndex-startIndex)+1)
	{
		m = ((endIndex-startIndex)+1)/2;
	}
	node *leftmax = pairmax(arr,startIndex,startIndex+m-1);
	node *rightmax = pairmax(arr,startIndex+m,endIndex);
	node *totalmax = newNode(maximum(leftmax->value, rightmax->value),leftmax,rightmax);
	return totalmax;
}