示例#1
0
HiveReturn HiveColumnsResultSet::initialize(HiveConnection* connection,
        const char* tbl_search_pattern,
        const char* col_search_pattern, char* err_buf,
        size_t err_buf_len) {
    RETURN_ON_ASSERT(connection == NULL, __FUNCTION__,
                     "Hive connection cannot be NULL.", err_buf, err_buf_len, HIVE_ERROR);
    RETURN_ON_ASSERT(connection->client == NULL, __FUNCTION__,
                     "Hive connection client cannot be NULL.", err_buf, err_buf_len, HIVE_ERROR);
    RETURN_ON_ASSERT(tbl_search_pattern == NULL, __FUNCTION__,
                     "Table search pattern cannot be NULL.", err_buf, err_buf_len, HIVE_ERROR);
    RETURN_ON_ASSERT(col_search_pattern == NULL, __FUNCTION__,
                     "Column search pattern cannot be NULL.", err_buf, err_buf_len, HIVE_ERROR);

    /* TODO: col_search_pattern is not currently supported; arg is ignored for now;
     * either add support in Hive Server or here */

    m_connection = connection;
    m_tbl_fetch_idx = -1;
    m_col_fetch_idx = -1;

    try {
        /* Just use the default database name for now b/c Hive does not yet support multiple
         * databases */
        connection->client->get_tables(m_tables, DEFAULT_DATABASE, tbl_search_pattern);
    } catch (Apache::Hadoop::Hive::MetaException& ex) {
        RETURN_FAILURE(__FUNCTION__, ex.what(), err_buf, err_buf_len, HIVE_ERROR);
    } catch (...) {
        RETURN_FAILURE(__FUNCTION__,
                       "Unknown Hive get tables error.", err_buf, err_buf_len, HIVE_ERROR);
    }
    /* Sort the table names */
    sort(m_tables.begin(), m_tables.end());

    return initializeSchema(err_buf, err_buf_len);
}
示例#2
0
HiveReturn HiveColumnsResultSet::getNextTableFields(char* err_buf, size_t err_buf_len) {
    /* Clear out the field schemas for the previous table */
    m_columns.clear();

    m_tbl_fetch_idx++;
    if (m_tbl_fetch_idx >= (int) m_tables.size()) /* If there are no more tables */
    {
        /* Prevent the m_tbl_fetch_idx from wrapping around after too many calls */
        m_tbl_fetch_idx--;
        return HIVE_NO_MORE_DATA; /* No more data to fetch */
    }

    assert(m_connection != NULL);
    assert(m_connection->client != NULL);
    try {
        /* Just use the default database name for now b/c Hive does not yet support multiple databases */
        m_connection->client->get_schema(m_columns, DEFAULT_DATABASE, m_tables[m_tbl_fetch_idx]);
    } catch (Apache::Hadoop::Hive::MetaException& ex) {
        RETURN_FAILURE(__FUNCTION__, ex.what(), err_buf, err_buf_len, HIVE_ERROR);
    } catch (Apache::Hadoop::Hive::UnknownTableException& ex) {
        RETURN_FAILURE(__FUNCTION__, ex.what(), err_buf, err_buf_len, HIVE_ERROR);
    } catch (Apache::Hadoop::Hive::UnknownDBException& ex) {
        RETURN_FAILURE(__FUNCTION__, ex.what(), err_buf, err_buf_len, HIVE_ERROR);
    } catch (...) {
        RETURN_FAILURE(__FUNCTION__,
                       "Unknown Hive get fields error.", err_buf, err_buf_len, HIVE_ERROR);
    }
    assert(m_columns.size() > 0); /* Every table must have at least one column */

    return HIVE_SUCCESS;
}
示例#3
0
HiveReturn HiveQueryResultSet::initializeSchema(char* err_buf, size_t err_buf_len) {
    try {
        m_connection->client->getSchema(m_schema);
    } catch (Apache::Hadoop::Hive::HiveServerException& ex) {
        RETURN_FAILURE(__FUNCTION__, ex.what(), err_buf, err_buf_len, HIVE_ERROR);
    } catch (...) {
        RETURN_FAILURE(__FUNCTION__,
                       "Unknown Hive get result schema error.", err_buf, err_buf_len, HIVE_ERROR);
    }

    /* TODO: hard code this in for now because m_schema.properties not properly implemented;
     * but remove this when it is implemented */
    m_schema.properties[FIELD_DELIM] = "\t";
    m_schema.properties[SERIALIZATION_NULL_FORMAT] = DEFAULT_NULL_FORMAT;

    /* TODO: replace the real null representation with 'NULL' because of a bug in the Hive Server
     * fetch function; remove this when Hive Server has been fixed to not replace the actual null
     * rep with NULL. */
    m_schema.properties[SERIALIZATION_NULL_FORMAT] = "NULL";

    /* Verify the presence of known m_schema properties */
    assert(m_schema.properties.find(FIELD_DELIM) != m_schema.properties.end());
    assert(m_schema.properties.find(SERIALIZATION_NULL_FORMAT) != m_schema.properties.end());

    return HIVE_SUCCESS;
}
	double parse_factors(char *& expression)
	{
		FAIL_CHECK;
		double num1 = parse_atom(expression);
		FAIL_CHECK;
		for (;;)
		{
			skip_spaces(expression);
			char op = *expression;
			char *position = expression;
			if (op != '/' && op != '*')
			{
				// nothing for us, pass it back
				return num1;
			}
			expression++;
			double num2 = parse_atom(expression);
			FAIL_CHECK;
			if (op == '/')
			{
				if (num2 == 0)
				{
					RETURN_FAILURE(DIV_ZERO, expression);
				}
				num1 /= num2;
			}
			else
			{
				num1 *= num2;
			}
		}
	}
	double parse_atom(char *& expression)
	{
		FAIL_CHECK;

		skip_spaces(expression);

		bool negative = false;
		if (*expression == '-')
		{
			negative = true;
			expression++;
		}
		if (*expression == '+')
		{
			expression++;
		}

		// PAR-EN-THESIS
		if (*expression == ')' && !m_parenthesis)
		{
			RETURN_FAILURE(NO_OPEN_PARENTHESES, expression);
		}
		if (*expression == '(')
		{
			expression++;
			m_parenthesis++;
			double result = parse_commands(expression);
			if (*expression != ')')
			{
				RETURN_FAILURE(NO_CLOSE_PARENTHESES, expression);
			}
			expression++;
			m_parenthesis--;
			return negative ? -result : result;
		}

		// convert the string to a double, gives point in the string where the number ends
		char *end_ptr = nullptr;
		double res = strtod(expression, &end_ptr);
		expression = end_ptr;
		return negative ? -res : res;
	}
示例#6
0
文件: ff_func.cpp 项目: ozgend/hive
CSSM_RETURN dlbe_DeleteRelation(CSSM_DB_RECORDTYPE RelationID, DAL_MODULE_PARAMETERS const * Parameters)
{
	FF_FUNCTION_BEGIN(dlbe_DeleteDatabase);

	ASSERT(Parameters);
	ASSERT(GetPathStart(Parameters) && GetPathStartLength(Parameters));

	RETURN_FAILURE(ffport_eDeleteDbRelationFiles(RelationID, GetPathStart(Parameters)));

	return CSSM_OK;
}
示例#7
0
文件: ff_func.cpp 项目: ozgend/hive
/*-----------------------------------------------------------------------------
 * Name: dlbe_DeleteDatabase
 *
 * Description:
 * This function removes a datastore from flat file and delete the access file
 *
 * Parameters:
 * DbName (input) : The name of the database to delete
 *
 * Return value:
 * CSSM_OK on success, otherwise CSSM_FAIL
 *
 * Error Codes:
 * TODO
 *---------------------------------------------------------------------------*/
CSSM_RETURN dlbe_DeleteDatabase(const char *DbName, DAL_MODULE_PARAMETERS const * Parameters)
{
	FF_FUNCTION_BEGIN(dlbe_DeleteDatabase);

	ASSERT(DbName);
	ASSERT(Parameters);
	ASSERT(GetPathStart(Parameters) && GetPathStartLength(Parameters));

	RETURN_FAILURE(ffport_eDeleteDbFiles(GetPathStart(Parameters)));

	return CSSM_OK;
}
示例#8
0
int main (void)
{
  ncptl_int i;

  debug_printf ("\tTesting ncptl_func_bits() ...\n");
  for (i=0; i<BIGNUM; i++) {
    ncptl_int numbits = ncptl_func_bits (i);

    if (1<<numbits < i) {
      debug_printf ("\t   ncptl_func_bits(%" NICS ") --> %" NICS "  [too small]\n",
                    i, numbits);
      RETURN_FAILURE();
    }
    if (1<<(numbits-1) > i) {
      debug_printf ("\t   ncptl_func_bits(%" NICS ") --> %" NICS "  [too large]\n",
                    i, numbits);
      RETURN_FAILURE();
    }
  }

  RETURN_SUCCESS();
}
示例#9
0
HiveReturn HiveQueryResultSet::fetchNewResults(char* err_buf, size_t err_buf_len) {
    m_result_set_data.clear(); /* Empty the original buffer just to be safe */
    assert(m_connection != NULL);
    assert(m_connection->client != NULL);
    assert(m_max_buffered_rows > 0);
    try {
        m_connection->client->fetchN(m_result_set_data, m_max_buffered_rows);
    } catch (Apache::Hadoop::Hive::HiveServerException& ex) {
        RETURN_FAILURE(__FUNCTION__, ex.what(), err_buf, err_buf_len, HIVE_ERROR);
    } catch (...) {
        RETURN_FAILURE(__FUNCTION__,
                       "Unknown Hive FetchN error.", err_buf, err_buf_len, HIVE_ERROR);
    }

    /* This indicates that a Hive server fetch call has successfully executed */
    m_fetch_attempted = true;
    if (!m_result_set_data.empty()) {
        /* This is initialized to be false and will remain true forever once this is set */
        m_has_results = true;
    }
    m_fetch_idx = -1; /* Reset the cursor b/c the old index no longer has any meaning */
    return HIVE_SUCCESS;
}
示例#10
0
int main (void)
{
  ncptl_int n, d;

  debug_printf ("\tTesting ncptl_func_modulo() ...\n");
  for (n=-10; n<10; n++)
    for (d=-10; d<10; d++)
      if (d) {
	ncptl_int result = ncptl_func_modulo (n, d);   /* n modulo d */
	ncptl_int some_integer;   /* Integer that might make the modulo expression true */
	int found_match = 0;      /* 1 if some_integer makes the modulo expression true */

	/* coNCePTuaL guarantees a positive remainder. */
	if (result < 0) {
	  debug_printf ("\t   ncptl_func_modulo (%" NICS ", %" NICS ") --> %" NICS "  [should not be negative]\n",
			n, d, result);
	  RETURN_FAILURE();
	}

	/* n=result (mod d) <--> n-result = some_integer*d for some
	 * some_integer. */
	for (some_integer=-ncptl_func_abs(n); some_integer<=ncptl_func_abs(n); some_integer++)
	  if (n-result == some_integer*d) {
	    found_match = 1;
	    break;
	  }
	if (!found_match) {
	  debug_printf ("\t   ncptl_func_modulo (%" NICS ", %" NICS ") --> %" NICS "  [incorrect result]\n",
			n, d, result);
	  RETURN_FAILURE();
	}
	debug_printf ("\t   ncptl_func_modulo (%" NICS ", %" NICS ") --> %" NICS "\n",
			n, d, result);
      }

  RETURN_SUCCESS();
}
示例#11
0
int main (int argc, char *argv[])
{
  uint64_t starttime, stoptime, elapsedtime;
  double timing_error;
  const double error_threshold = 5.0;    /* Allow a 5% error. */
  int i;

  /* Initialize the run-time library. */
  debug_printf ("\tTesting ncptl_time() ...\n");
  ncptl_init (NCPTL_RUN_TIME_VERSION, argv[0]);

  /* Measure what should be about a million microseconds. */
  for (i=3; i>0; i--) {
    starttime = ncptl_time();
    sleep (1);
    stoptime = ncptl_time();
    elapsedtime = stoptime - starttime;

    /* Complain if we're far off. */
    timing_error = 100.0 * fabs (((double)elapsedtime-1.0e+6) / 1.0e+6);
    debug_printf ("\t   Starting time (usecs):  %25" PRIu64 "\n", starttime);
    debug_printf ("\t   Ending time (usecs):    %25" PRIu64 "\n", stoptime);
    debug_printf ("\t   Elapsed time (usecs):   %25" PRIu64 "\n", elapsedtime);
    debug_printf ("\t   Expected value (usecs): %25llu\n", 1000000ULL);
    debug_printf ("\t   Error:                  %27.1lf%%\n", timing_error);
    if (timing_error <= error_threshold)
      RETURN_SUCCESS();
    if (i > 1)
      debug_printf ("\tTrying again ...\n");
    else
      debug_printf ("\tGiving up.\n");
  }

  /* Return successfully. */
  ncptl_finalize();
  argc = 0;        /* Try to avoid "unused parameter" warnings. */
  RETURN_FAILURE();
}
示例#12
0
Datum
kafka_consume_begin_all(PG_FUNCTION_ARGS)
{
	HeapTuple tup = NULL;
	HeapScanDesc scan;
	Relation consumers = open_pipeline_kafka_consumers();

	scan = heap_beginscan(consumers, GetTransactionSnapshot(), 0, NULL);
	while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
	{
		Oid id = HeapTupleGetOid(tup);
		KafkaConsumer consumer;

		load_consumer_state(id, &consumer);
		if (!launch_consumer_group(consumers, &consumer, RD_KAFKA_OFFSET_END))
			RETURN_FAILURE();
	}

	heap_endscan(scan);
	heap_close(consumers, NoLock);

	RETURN_SUCCESS();
}
示例#13
0
Datum
kafka_consume_begin_tr(PG_FUNCTION_ARGS)
{
	text *topic;
	text *qualified_name;
	RangeVar *relname;
	Relation rel;
	Relation consumers;
	Oid id;
	bool result;
	text *format;
	text *delimiter;
	text *quote;
	text *escape;
	int batchsize;
	int parallelism;
	int64 offset;
	KafkaConsumer consumer;

	if (PG_ARGISNULL(0))
		elog(ERROR, "topic cannot be null");
	if (PG_ARGISNULL(1))
		elog(ERROR, "relation cannot be null");
	if (PG_ARGISNULL(2))
		elog(ERROR, "format cannot be null");

	topic = PG_GETARG_TEXT_P(0);
	qualified_name = PG_GETARG_TEXT_P(1);
	format = PG_GETARG_TEXT_P(2);

	if (PG_ARGISNULL(3))
		delimiter = NULL;
	else
		delimiter = PG_GETARG_TEXT_P(3);

	if (PG_ARGISNULL(4))
		quote = NULL;
	else
		quote = PG_GETARG_TEXT_P(4);

	if (PG_ARGISNULL(5))
		escape = NULL;
	else
		escape = PG_GETARG_TEXT_P(5);

	if (PG_ARGISNULL(6))
		batchsize = 1000;
	else
		batchsize = PG_GETARG_INT32(6);

	if (PG_ARGISNULL(7))
		parallelism = 1;
	else
		parallelism = PG_GETARG_INT32(7);

	if (PG_ARGISNULL(8))
		offset = RD_KAFKA_OFFSET_NULL;
	else
		offset = PG_GETARG_INT64(8);

	/* there's no point in progressing if there aren't any brokers */
	if (!get_all_brokers())
		elog(ERROR, "add at least one broker with kafka_add_broker");

	/* verify that the target relation actually exists */
	relname = makeRangeVarFromNameList(textToQualifiedNameList(qualified_name));
	rel = heap_openrv(relname, NoLock);

	if (IsInferredStream(RelationGetRelid(rel)))
		ereport(ERROR,
				(errmsg("target stream must be static"),
				errhint("Use CREATE STREAM to create a stream that can consume a Kafka topic.")));

	heap_close(rel, NoLock);

	consumers = open_pipeline_kafka_consumers();
	id = create_or_update_consumer(consumers, qualified_name, topic, format,
			delimiter, quote, escape, batchsize, parallelism);
	load_consumer_state(id, &consumer);
	result = launch_consumer_group(consumers, &consumer, offset);
	heap_close(consumers, NoLock);

	if (result)
		RETURN_SUCCESS();
	else
		RETURN_FAILURE();
}
示例#14
0
int main (int argc, char *argv[])
{
  char *test_argv[6]; /* Hardwired argv[] for testing. */
  ncptl_int testvar;  /* Variable that ncptl_parse_command_line() should set */
  char *stringvar;    /* A string variable for ncptl_parse_command_line() */
  NCPTL_CMDLINE arglist[] = {    /* Arguments to test. */
    { NCPTL_TYPE_INT,
      NULL,
      "testing",
      't',
      "Test of ncptl_parse_command_line()",
      {0}
    },
    { NCPTL_TYPE_STRING,
      NULL,
      "somestring",
      's',
      "Another test of ncptl_parse_command_line()",
      {0}
    }
  };

  /* Initialize the run-time library and the ARGLIST array. */
  debug_printf ("\tTesting ncptl_parse_command_line() ...\n");
  ncptl_fast_init = 1;    /* We don't need accurate timing for this test. */
  ncptl_init (NCPTL_RUN_TIME_VERSION, argv[0]);
  arglist[0].variable = (CMDLINE_VALUE *) &testvar;
  arglist[0].defaultvalue.intval = 123;
  arglist[1].variable = (CMDLINE_VALUE *) &stringvar;
  arglist[1].defaultvalue.stringval = "abc123";

  /* Ensure that testvar receives its default value when given an
   * empty command line. */
  test_argv[0] = argv[0];
  test_argv[1] = NULL;
  testvar = 999;
  stringvar = "xxx999";
  ncptl_parse_command_line(1, test_argv, arglist, 2);
  debug_printf ("\tExpected 123;      got %" NICS ".\n", testvar);
  debug_printf ("\tExpected \"abc123\"; got \"%s\".\n", stringvar);
  if (testvar != 123)
    RETURN_FAILURE();
  if (strcmp (stringvar, "abc123"))
    RETURN_FAILURE();

  /* Ensure that short arguments work. */
  test_argv[1] = "-t";
  test_argv[2] = "456";
  test_argv[3] = "-s";
  test_argv[4] = "def456";
  test_argv[5] = NULL;
  testvar = 999;
  stringvar = "xxx999";
  ncptl_parse_command_line(5, test_argv, arglist, 2);
  debug_printf ("\tExpected 456;      got %" NICS ".\n", testvar);
  debug_printf ("\tExpected \"def456\"; got \"%s\".\n", stringvar);
  if (testvar != 456)
    RETURN_FAILURE();
  if (strcmp (stringvar, "def456"))
    RETURN_FAILURE();

  /* Ensure that long arguments work. */
#if defined(USE_POPT) || defined(USE_GETOPT_LONG)
  test_argv[1] = "--testing";
  test_argv[2] = "789";
  test_argv[3] = "--somestring";
  test_argv[4] = "ghi789";
  test_argv[5] = NULL;
  testvar = 999;
  stringvar = "xxx999";
  ncptl_parse_command_line(5, test_argv, arglist, 2);
  debug_printf ("\tExpected 789;      got %" NICS ".\n", testvar);
  debug_printf ("\tExpected \"ghi789\"; got \"%s\".\n", stringvar);
  if (testvar != 789)
    RETURN_FAILURE();
  if (strcmp (stringvar, "ghi789"))
    RETURN_FAILURE();
#endif

  /* Ensure that suffixed arguments work. */
  test_argv[1] = "-t";
  test_argv[2] = "1011e+2";
  test_argv[3] = NULL;
  testvar = 999;
  ncptl_parse_command_line(3, test_argv, arglist, 2);
  debug_printf ("\tExpected 101100;      got %" NICS ".\n", testvar);
  if (testvar != 101100)
    RETURN_FAILURE();

  /* Return successfully. */
  ncptl_finalize();
  argc = 0;        /* Try to avoid "unused parameter" warnings. */
  RETURN_SUCCESS();
}
示例#15
0
int main (void)
{
  ncptl_int parent2[] = {        /* Map to parent in a 2-ary tree */
    -1,
    0, 0,
    1, 1,
    2, 2,
    3, 3,
    4, 4,
    5, 5,
    6, 6
  };
  ncptl_int parent3[] = {        /* Map to parent in a 3-ary tree */
    -1,
    0, 0, 0,
    1, 1, 1,
    2, 2, 2,
    3, 3, 3,
    4, 4, 4,
    5, 5, 5,
    6, 6, 6
  };
  ncptl_int child2[][2] = {      /* Map to children in a 2-ary tree */
    { 1,  2},
    { 3,  4},
    { 5,  6},
    { 7,  8},
    { 9, 10},
    {11, 12},
    {13, 14}
  };
  ncptl_int child3[][3] = {      /* Map to children in a 3-ary tree */
    { 1,  2,  3},
    { 4,  5,  6},
    { 7,  8,  9},
    {10, 11, 12}
  };
  ncptl_int mesh_neighbor_pos[] = {   /* Map to {+x, +y, +z} neighbor in a 4x3x2 mesh */
    17, 18, 19, -1,
    21, 22, 23, -1,
    -1, -1, -1, -1,
    -1, -1, -1, -1,
    -1, -1, -1, -1,
    -1, -1, -1, -1
  };
  ncptl_int torus_neighbor_pos[] = {  /* Map to {+x, +y, +z} neighbor in a 4x3x2 torus */
    17, 18, 19, 16,
    21, 22, 23, 20,
    13, 14, 15, 12,
     5,  6,  7,  4,
     9, 10, 11,  8,
     1,  2,  3,  0
  };
  ncptl_int partial_torus_neighbor_pos[] = {  /* Map to {+x, +y, +z} neighbor in a 4x3x2 mesh that wraps in y only */
    17, 18, 19, -1,
    21, 22, 23, -1,
    13, 14, 15, -1,
    -1, -1, -1, -1,
    -1, -1, -1, -1,
    -1, -1, -1, -1
  };
  ncptl_int parent2k[] = {      /* Map to parent in a 2-nomial tree */
    -1, 0,  0, 1,
     0, 1,  2, 3
  };
  ncptl_int parent3k[] = {      /* Map to parent in a 3-nomial tree */
    -1,  0,  0,   0,  1,  2,   0,  1,  2,
     0,  1,  2,   3,  4,  5,   6,  7,  8,
     0,  1,  2,   3,  4,  5,   6,  7,  8
  };
  ncptl_int child2k[][3] = {    /* Map to children in a 2-nomial tree */
    { 1,  2,  4},
    { 3,  5, -1},
    { 6, -1, -1},
    { 7, -1, -1},
    {-1, -1, -1},
    {-1, -1, -1},
    {-1, -1, -1},
    {-1, -1, -1}
  };
  ncptl_int child3k[][6] = {    /* Map to children in a 3-nomial tree */
    { 1,  2,  3,   6,  9, 18},
    { 4,  7, 10,  19, -1, -1},
    { 5,  8, 11,  20, -1, -1},

    {12, 21, -1,  -1, -1, -1},
    {13, 22, -1,  -1, -1, -1},
    {14, 23, -1,  -1, -1, -1},

    {15, 24, -1,  -1, -1, -1},
    {16, 25, -1,  -1, -1, -1},
    {17, 26, -1,  -1, -1, -1},

    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},

    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},

    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},

    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},

    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},

    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1},
    {-1, -1, -1,  -1, -1, -1}
  };
  ncptl_int knomial_sizes[3];   /* Three different tree sizes to try */
  ncptl_int i, j, k;
  ncptl_int x, y, z;

  /* Test ncptl_func_tree_parent(). */
  debug_printf ("\tTesting ncptl_func_tree_parent() ...\n");
  for (i=0; i<(ncptl_int)(sizeof(parent2)/sizeof(ncptl_int)); i++) {
    debug_printf ("\t   ncptl_func_tree_parent (%" NICS ", 2) --> %" NICS,
                  i, ncptl_func_tree_parent(i, 2));
    if (ncptl_func_tree_parent(i, 2) != parent2[i]) {
      debug_printf (" (should be %" NICS ")\n", parent2[i]);
      RETURN_FAILURE();
    }
    else
      debug_printf ("\n");
  }
  for (i=0; i<(ncptl_int)(sizeof(parent3)/sizeof(ncptl_int)); i++) {
    debug_printf ("\t   ncptl_func_tree_parent (%" NICS ", 3) --> %" NICS,
                  i, ncptl_func_tree_parent(i, 3));
    if (ncptl_func_tree_parent(i, 3) != parent3[i]) {
      debug_printf (" (should be %" NICS ")\n", parent3[i]);
      RETURN_FAILURE();
    }
    else
      debug_printf ("\n");
  }
  debug_printf ("\n");

  /* Test ncptl_func_tree_child(). */
  debug_printf ("\tTesting ncptl_func_tree_child() ...\n");
  for (i=0; i<(ncptl_int)(sizeof(child2)/(2*sizeof(ncptl_int))); i++)
    for (j=0; j<2; j++) {
      debug_printf ("\t   ncptl_func_tree_child (%" NICS ", %" NICS ", 2) --> %" NICS,
                    i, j, ncptl_func_tree_child(i, j, 2));
      if (ncptl_func_tree_child(i, j, 2) != child2[i][j]) {
        debug_printf (" (should be %" NICS ")\n", child2[i][j]);
        RETURN_FAILURE();
      }
      else
        debug_printf ("\n");
    }
  for (i=0; i<(ncptl_int)(sizeof(child3)/(3*sizeof(ncptl_int))); i++)
    for (j=0; j<3; j++) {
      debug_printf ("\t   ncptl_func_tree_child (%" NICS ", %" NICS ", 3) --> %" NICS,
                    i, j, ncptl_func_tree_child(i, j, 3));
      if (ncptl_func_tree_child(i, j, 3) != child3[i][j]) {
        debug_printf (" (should be %" NICS ")\n", child3[i][j]);
        RETURN_FAILURE();
      }
      else
        debug_printf ("\n");
    }
  debug_printf ("\n");

  /* Test ncptl_func_mesh_neighbor(). */
  debug_printf ("\tTesting ncptl_func_mesh_neighbor() ...\n");
  for (i=0; i<(ncptl_int)(sizeof(mesh_neighbor_pos)/sizeof(ncptl_int)); i++) {
    ncptl_int neighbor = ncptl_func_mesh_neighbor (4, 3, 2,
                                                   0, 0, 0,
                                                   i,
                                                   +1, +1, +1);
    debug_printf ("\t   ncptl_func_mesh_neighbor (4, 3, 2, 0, 0, 0, %2" NICS
                  ", +1, +1, +1) --> %3" NICS,
                  i, neighbor);
    if (mesh_neighbor_pos[i] != neighbor) {
      debug_printf (" (should be %" NICS ")\n", mesh_neighbor_pos[i]);
      RETURN_FAILURE();
    }
    else
      debug_printf ("\n");
  }
  for (i=0; i<(ncptl_int)(sizeof(torus_neighbor_pos)/sizeof(ncptl_int)); i++) {
    ncptl_int neighbor = ncptl_func_mesh_neighbor (4, 3, 2,
                                                   1, 1, 1,
                                                   i,
                                                   +1, +1, +1);
    debug_printf ("\t   ncptl_func_mesh_neighbor (4, 3, 2, 1, 1, 1, %2" NICS
                  ", +1, +1, +1) --> %3" NICS,
                  i, neighbor);
    if (torus_neighbor_pos[i] != neighbor) {
      debug_printf (" (should be %" NICS ")\n", torus_neighbor_pos[i]);
      RETURN_FAILURE();
    }
    else
      debug_printf ("\n");
  }
  for (i=0; i<(ncptl_int)(sizeof(partial_torus_neighbor_pos)/sizeof(ncptl_int)); i++) {
    ncptl_int neighbor = ncptl_func_mesh_neighbor (4, 3, 2,
                                                   0, 1, 0,
                                                   i,
                                                   +1, +1, +1);
    debug_printf ("\t   ncptl_func_mesh_neighbor (4, 3, 2, 0, 1, 0, %2" NICS
                  ", +1, +1, +1) --> %3" NICS,
                  i, neighbor);
    if (partial_torus_neighbor_pos[i] != neighbor) {
      debug_printf (" (should be %" NICS ")\n", partial_torus_neighbor_pos[i]);
      RETURN_FAILURE();
    }
    else
      debug_printf ("\n");
  }
  debug_printf ("\n");

  /* Test ncptl_func_mesh_coord(). */
  debug_printf ("\tTesting ncptl_func_mesh_coord() ...\n");
  for (z=0; z<GRIDDEPTH; z++)
    for (y=0; y<GRIDHEIGHT; y++)
      for (x=0; x<GRIDWIDTH; x++) {
        ncptl_int taskID = x + GRIDWIDTH*(y + GRIDHEIGHT*z);
        ncptl_int coords[3];

        for (i=0; i<3; i++)
          coords[i] =
            ncptl_func_mesh_coord (GRIDWIDTH, GRIDHEIGHT, GRIDDEPTH, taskID, i);
        debug_printf ("\t   ncptl_func_mesh_coord (%d, %d, %d, %2" NICS
                      ", {0,1,2}) --> {%" NICS ",%" NICS ",%" NICS "}",
                      GRIDWIDTH, GRIDHEIGHT, GRIDDEPTH, taskID,
                      coords[0], coords[1], coords[2]);
        if (x!=coords[0] || y!=coords[1] || z!=coords[2]) {
          debug_printf (" (should be {%" NICS ",%" NICS ",%" NICS "})\n",
                        x, y, z);
          RETURN_FAILURE();
        }
        debug_printf ("\n");
      }
  debug_printf ("\n");

  /* Test ncptl_func_mesh_distance(). */
  debug_printf ("\tTesting ncptl_func_mesh_distance() ...\n");
  for (z=0; z<GRIDDEPTH; z++)
    for (y=0; y<GRIDHEIGHT; y++)
      for (x=0; x<GRIDWIDTH; x++) {
        ncptl_int taskID_1 = x + GRIDWIDTH*(y + GRIDHEIGHT*z);
        ncptl_int xdelta, ydelta, zdelta;

        for (zdelta=0; zdelta<GRIDDEPTH; zdelta++)
          for (ydelta=0; ydelta<GRIDHEIGHT; ydelta++)
            for (xdelta=0; xdelta<GRIDWIDTH; xdelta++) {
              ncptl_int newz = (z + zdelta) % GRIDDEPTH;
              ncptl_int newy = (y + ydelta) % GRIDHEIGHT;
              ncptl_int newx = (x + xdelta) % GRIDWIDTH;
              ncptl_int abs_xdelta = (x <= newx) ? xdelta : GRIDWIDTH - xdelta;
              ncptl_int abs_ydelta = (y <= newy) ? ydelta : GRIDHEIGHT - ydelta;
              ncptl_int abs_zdelta = (z <= newz) ? zdelta : GRIDDEPTH - zdelta;
              ncptl_int taskID_2 = newx + GRIDWIDTH*(newy + GRIDHEIGHT*newz);
              ncptl_int expected_meshdist;
              ncptl_int meshdist;
              ncptl_int expected_torusdist;
              ncptl_int torusdist;

              /* Determine the correct distances. */
              expected_meshdist = abs_xdelta + abs_ydelta + abs_zdelta;
              expected_torusdist = 0;
              expected_torusdist += abs_xdelta <= GRIDWIDTH/2 ? abs_xdelta : GRIDWIDTH - abs_xdelta;
              expected_torusdist += abs_ydelta <= GRIDHEIGHT/2 ? abs_ydelta : GRIDHEIGHT - abs_ydelta;
              expected_torusdist += abs_zdelta <= GRIDDEPTH/2 ? abs_zdelta : GRIDDEPTH - abs_zdelta;

              /* Validate distance on a mesh. */
              meshdist =
                ncptl_func_mesh_distance (GRIDWIDTH, GRIDHEIGHT, GRIDDEPTH,
                                          0, 0, 0,
                                          taskID_1, taskID_2);
              debug_printf ("\t   ncptl_func_mesh_distance (%d, %d, %d, 0, 0, 0, %" NICS
                            ", %" NICS ") --> %" NICS,
                            GRIDWIDTH, GRIDHEIGHT, GRIDDEPTH,
                            taskID_1, taskID_2, meshdist);
              if (meshdist != expected_meshdist) {
                debug_printf (" (should be %" NICS ")\n", expected_meshdist);
                RETURN_FAILURE();
              }
              debug_printf ("\n");

              /* Validate distance on a full torus. */
              torusdist =
                ncptl_func_mesh_distance (GRIDWIDTH, GRIDHEIGHT, GRIDDEPTH,
                                          1, 1, 1,
                                          taskID_1, taskID_2);
              debug_printf ("\t   ncptl_func_mesh_distance (%d, %d, %d, 1, 1, 1, %" NICS
                            ", %" NICS ") --> %" NICS,
                            GRIDWIDTH, GRIDHEIGHT, GRIDDEPTH,
                            taskID_1, taskID_2, torusdist);
              if (torusdist != expected_torusdist) {
                debug_printf (" (should be %" NICS ")\n", expected_torusdist);
                RETURN_FAILURE();
              }
              debug_printf ("\n");
            }
      }
  debug_printf ("\n");

  /* Test ncptl_func_knomial_parent(). */
  debug_printf ("\tTesting ncptl_func_knomial_parent() ...\n");
  knomial_sizes[0] = sizeof(parent2k)/sizeof(ncptl_int);
  knomial_sizes[1] = 1000;
  knomial_sizes[2] = knomial_sizes[0] - 1;
  for (j=0; j<3; j++) {
    if (j == 2)
      parent2k[sizeof(parent2k)/sizeof(ncptl_int) - 1] = -1;
    for (i=0; i<(ncptl_int)(sizeof(parent2k)/sizeof(ncptl_int)); i++) {
      debug_printf ("\t   ncptl_func_knomial_parent (%" NICS ", 2, %" NICS ") --> %" NICS,
                    i, knomial_sizes[j],
                    ncptl_func_knomial_parent(i, 2, knomial_sizes[j]));
      if (ncptl_func_knomial_parent(i, 2, knomial_sizes[j]) != parent2k[i]) {
        debug_printf (" (should be %" NICS ")\n", parent2k[i]);
        RETURN_FAILURE();
      }
      else
        debug_printf ("\n");
    }
  }
  knomial_sizes[0] = sizeof(parent3k)/sizeof(ncptl_int);
  knomial_sizes[1] = 1000;
  knomial_sizes[2] = knomial_sizes[0] - 1;
  for (j=0; j<3; j++) {
    if (j == 2)
      parent3k[sizeof(parent3k)/sizeof(ncptl_int) - 1] = -1;
    for (i=0; i<(ncptl_int)(sizeof(parent3k)/sizeof(ncptl_int)); i++) {
      debug_printf ("\t   ncptl_func_knomial_parent (%" NICS ", 3, %" NICS ") --> %" NICS,
                    i, knomial_sizes[j],
                    ncptl_func_knomial_parent(i, 3, knomial_sizes[j]));
      if (ncptl_func_knomial_parent(i, 3, knomial_sizes[j]) != parent3k[i]) {
        debug_printf (" (should be %" NICS ")\n", parent3k[i]);
        RETURN_FAILURE();
      }
      else
        debug_printf ("\n");
    }
  }
  debug_printf ("\n");

  /* Test ncptl_func_knomial_child(). */
  knomial_sizes[0] = sizeof(child2k)/(3*sizeof(ncptl_int));
  knomial_sizes[1] = knomial_sizes[0] - 1;
  debug_printf ("\tTesting ncptl_func_knomial_child() ...\n");
  for (k=0; k<2; k++) {
    if (k == 1)
      child2k[3][0] = -1;
    for (i=0; i<(ncptl_int)(sizeof(child2k)/(3*sizeof(ncptl_int))); i++)
      for (j=0; j<2; j++) {
        ncptl_int result = ncptl_func_knomial_child(i, j, 2, knomial_sizes[k], 0);
        debug_printf ("\t   ncptl_func_knomial_child (%" NICS ", %" NICS
                      ", 2, %" NICS ", 0) --> %" NICS,
                      i, j, knomial_sizes[k], result);
        if (result != child2k[i][j]) {
          debug_printf (" (should be %" NICS ")\n", child2k[i][j]);
          RETURN_FAILURE();
        }
        else
          debug_printf ("\n");
      }
  }
  knomial_sizes[0] = sizeof(child3k)/(6*sizeof(ncptl_int));
  knomial_sizes[1] = knomial_sizes[0] - 1;
  for (k=0; k<2; k++) {
    if (k == 1)
      child3k[8][1] = -1;
    for (i=0; i<(ncptl_int)(sizeof(child3k)/(6*sizeof(ncptl_int))); i++)
      for (j=0; j<2; j++) {
        ncptl_int result = ncptl_func_knomial_child(i, j, 3, knomial_sizes[k], 0);
        debug_printf ("\t   ncptl_func_knomial_child (%" NICS ", %" NICS
                      ", 3, %" NICS ", 0) --> %" NICS,
                      i, j, knomial_sizes[k], result);
        if (result != child3k[i][j]) {
          debug_printf (" (should be %" NICS ")\n", child3k[i][j]);
          RETURN_FAILURE();
        }
        else
          debug_printf ("\n");
      }
  }

  RETURN_SUCCESS();
}