2004-08-13 03:07:21 +04:00
|
|
|
/*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/param.h>
|
2004-08-13 06:26:12 +04:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2004-08-13 03:07:21 +04:00
|
|
|
|
|
|
|
#include "ompi_config.h"
|
|
|
|
#include "util/sys_info.h"
|
|
|
|
#include "support.h"
|
|
|
|
#include "../src/util/pack.h"
|
|
|
|
#include "../src/include/constants.h"
|
|
|
|
|
|
|
|
/* used for debugging */
|
|
|
|
/* int dump_buf (ompi_buffer_t buf); */
|
|
|
|
|
|
|
|
ompi_buffer_t bufA;
|
|
|
|
ompi_buffer_t bufB;
|
|
|
|
ompi_buffer_t bufC;
|
|
|
|
|
|
|
|
|
|
|
|
static bool test1(void); /* verify different buffer inits */
|
|
|
|
static bool test2(void); /* verify we can pack ok */
|
2004-08-18 18:12:33 +04:00
|
|
|
static bool test3(void); /* verify we can pack expanding buf */
|
2004-08-13 03:07:21 +04:00
|
|
|
static bool test4(void); /* verify pack a packed buffer */
|
|
|
|
static bool test5(void); /* verify unpack */
|
2004-08-13 06:26:12 +04:00
|
|
|
static bool test6(void); /* verify free */
|
|
|
|
static bool test7(void); /* verify preallocated buffer init, pack and unpack */
|
2004-08-19 03:18:25 +04:00
|
|
|
static bool test8(void); /* verify string pack and unpack */
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
int main (int argc, char* argv[])
|
2004-08-13 03:07:21 +04:00
|
|
|
{
|
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
test_init("ompi_pack");
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (test1()) {
|
2004-08-13 03:07:21 +04:00
|
|
|
test_success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
test_failure("ompi_pack test1 failed");
|
2004-08-18 18:12:33 +04:00
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (test2()) {
|
2004-08-13 03:07:21 +04:00
|
|
|
test_success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
test_failure("ompi_pack test2 failed");
|
2004-08-18 18:12:33 +04:00
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (test3()) {
|
2004-08-13 03:07:21 +04:00
|
|
|
test_success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
test_failure("ompi_pack test3 failed");
|
2004-08-18 18:12:33 +04:00
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (test4()) {
|
2004-08-13 03:07:21 +04:00
|
|
|
test_success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
test_failure("ompi_pack test4 failed");
|
2004-08-18 18:12:33 +04:00
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (test5()) {
|
2004-08-13 03:07:21 +04:00
|
|
|
test_success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
test_failure("ompi_pack test5 failed");
|
2004-08-18 18:12:33 +04:00
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (test6()) {
|
2004-08-13 03:07:21 +04:00
|
|
|
test_success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
test_failure("ompi_pack test6 failed");
|
2004-08-18 18:12:33 +04:00
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (test7()) {
|
2004-08-13 06:26:12 +04:00
|
|
|
test_success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
test_failure("ompi_pack test7 failed");
|
2004-08-18 18:12:33 +04:00
|
|
|
}
|
2004-08-13 06:26:12 +04:00
|
|
|
|
2004-08-19 03:18:25 +04:00
|
|
|
if (test8()) {
|
|
|
|
test_success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
test_failure("ompi_pack test8 failed");
|
|
|
|
}
|
|
|
|
|
2004-08-13 06:26:12 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
/* if (testN()) { */
|
2004-08-13 03:07:21 +04:00
|
|
|
/* test_success(); */
|
|
|
|
/* } */
|
|
|
|
/* else { */
|
|
|
|
/* test_failure("ompi_pack testN failed"); */
|
2004-08-18 18:12:33 +04:00
|
|
|
/* } */
|
2004-08-13 03:07:21 +04:00
|
|
|
|
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
test_finalize();
|
|
|
|
return (0);
|
2004-08-13 03:07:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool test1(void) /* verify different buffer inits */
|
|
|
|
{
|
2004-08-18 18:12:33 +04:00
|
|
|
int rc;
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
rc = ompi_buffer_init (&bufA, 0);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_init failed"); return(false);}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
rc = ompi_buffer_init (&bufB, 16);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_init failed"); return(false);}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
rc = ompi_buffer_init (&bufC, 1024);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_init failed"); return(false);}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
return (true);
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
|
2004-08-13 03:07:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BufA should hold 1024 INT32s */
|
|
|
|
static bool test2(void) /* verify we can pack ok */
|
|
|
|
{
|
2004-08-18 18:12:33 +04:00
|
|
|
int rc;
|
|
|
|
int i;
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
for (i=0;i<1024;i++) {
|
|
|
|
rc = ompi_pack (bufA, &i, 1, OMPI_INT32);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack failed"); return(false);}
|
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
return (true);
|
2004-08-13 03:07:21 +04:00
|
|
|
}
|
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
|
2004-08-13 03:07:21 +04:00
|
|
|
/* BufB was init with 16 bytes not 1024 sizeof(INT32)s */
|
|
|
|
/* so it should expand and keep packing */
|
2004-08-18 18:12:33 +04:00
|
|
|
static bool test3(void) /* verify we can pack expanding buf */
|
2004-08-13 03:07:21 +04:00
|
|
|
{
|
2004-08-18 18:12:33 +04:00
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (i=0;i<1024;i++) {
|
|
|
|
j = i * 2; /* so we can verify */
|
|
|
|
rc = ompi_pack (bufB, &j, 1, OMPI_INT32);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack failed"); return(false);}
|
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
return (true);
|
2004-08-13 03:07:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool test4(void) /* verify pack a packed buffer */
|
|
|
|
{
|
2004-08-18 18:12:33 +04:00
|
|
|
int rc;
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
rc = ompi_pack (bufC, bufA, 1, OMPI_PACKED);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack failed"); return(false);}
|
|
|
|
rc = ompi_pack (bufC, bufB, 1, OMPI_PACKED);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack failed"); return(false);}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
return (true);
|
2004-08-13 03:07:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool test5(void) /* verify unpack */
|
|
|
|
{
|
2004-08-18 18:12:33 +04:00
|
|
|
int rc;
|
|
|
|
int i, j;
|
|
|
|
int out;
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
for (i=0;i<1024;i++) {
|
|
|
|
j = i; /* for bufA */
|
|
|
|
rc = ompi_unpack (bufA, &out, 1, OMPI_INT32);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_unpack failed"); return(false);}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (out!=j) { test_comment ("bufA packed != unpacked data"); return(false);}
|
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
for (i=0;i<1024;i++) {
|
|
|
|
j = i*2; /* for bufB */
|
|
|
|
rc = ompi_unpack (bufB, &out, 1, OMPI_INT32);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_unpack failed"); return(false);}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (out!=j) { test_comment ("bufB packed != unpacked data"); return(false);}
|
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
for (i=0;i<2048;i++) {
|
|
|
|
|
|
|
|
if (i<1024) { j = i; /* bufAs data 1st half */ }
|
|
|
|
else { j = (i-1024)*2; /* bufBs data 2nd half */ }
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
rc = ompi_unpack (bufC, &out, 1, OMPI_INT32);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_unpack failed"); return(false);}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
if (out!=j) {
|
|
|
|
test_comment ("bufC packed != unpacked data");
|
|
|
|
printf("iteration %d expected %d have %d\n", i, j, out);
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
return (true);
|
2004-08-13 03:07:21 +04:00
|
|
|
}
|
|
|
|
|
2004-08-13 06:26:12 +04:00
|
|
|
static bool test6(void) /* verify free */
|
2004-08-13 03:07:21 +04:00
|
|
|
{
|
2004-08-18 18:12:33 +04:00
|
|
|
int rc;
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
rc = ompi_buffer_free (bufA);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_free failed"); return(false);}
|
|
|
|
rc = ompi_buffer_free (bufB);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_free failed"); return(false);}
|
|
|
|
rc = ompi_buffer_free (bufC);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_free failed"); return(false);}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
return (true);
|
2004-08-13 03:07:21 +04:00
|
|
|
}
|
|
|
|
|
2004-08-13 06:26:12 +04:00
|
|
|
static bool test7(void) /* verify preallocated buffer init, pack and unpack */
|
|
|
|
{
|
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
int rc;
|
|
|
|
int i, j, out;
|
|
|
|
char *mybuf;
|
|
|
|
int *p;
|
2004-08-13 06:26:12 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* we cannot use heap/static memory for a buffer as it cannot be realloced as needed for this test */
|
|
|
|
mybuf = (char*) malloc (sizeof(int)*5);
|
2004-08-18 18:12:33 +04:00
|
|
|
p = (int*) mybuf;
|
|
|
|
for(i=0;i<5;i++) {
|
|
|
|
*p++ = htonl (i); /* the data must be in network byte order for this test to be valid */
|
|
|
|
/* as the test is emulating prepacked data recvd from the network (OOB) */
|
|
|
|
}
|
2004-08-13 06:26:12 +04:00
|
|
|
|
|
|
|
|
|
|
|
rc = ompi_buffer_init_preallocated (&bufA, mybuf, sizeof(int)*5);
|
2004-08-18 18:12:33 +04:00
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_init_preallocated failed"); return(false);}
|
2004-08-13 06:26:12 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
i = 5;
|
|
|
|
rc = ompi_pack (bufA, &i, 1, OMPI_INT32);
|
2004-08-13 06:26:12 +04:00
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack failed"); return(false);}
|
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
/* ok, now check its contents */
|
|
|
|
for (i=0;i<6;i++) {
|
|
|
|
j = i; /* for bufA */
|
|
|
|
rc = ompi_unpack (bufA, &out, 1, OMPI_INT32);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_unpack failed"); return(false);}
|
|
|
|
|
|
|
|
if (out!=j) {
|
|
|
|
test_comment ("bufA packed != unpacked data");
|
|
|
|
printf("iteration %d expected %d have %d\n", i, j, out);
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
}
|
2004-08-13 06:26:12 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
|
|
|
|
/* I do not free mybuf as ompi_buffer_free() will */
|
2004-08-13 06:26:12 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
rc = ompi_buffer_free (bufA);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_free failed"); return(false);}
|
2004-08-13 06:26:12 +04:00
|
|
|
|
2004-08-18 18:12:33 +04:00
|
|
|
return (true);
|
2004-08-13 06:26:12 +04:00
|
|
|
}
|
2004-08-13 03:07:21 +04:00
|
|
|
|
2004-08-19 03:18:25 +04:00
|
|
|
|
|
|
|
static bool test8(void) /* verify string pack and unpack */
|
|
|
|
{
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
char *str1;
|
|
|
|
char *str2;
|
|
|
|
|
|
|
|
rc = ompi_buffer_init (&bufA, 0);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_init failed"); return(false);}
|
|
|
|
|
|
|
|
rc = ompi_buffer_init (&bufB, 10);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_init failed"); return(false);}
|
|
|
|
|
|
|
|
rc = ompi_buffer_init (&bufC, 16);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_init failed"); return(false);}
|
|
|
|
|
|
|
|
rc = ompi_pack_string (bufA, "HELLO ");
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack_string failed"); return(false);}
|
|
|
|
|
|
|
|
rc = ompi_pack_string (bufB, "WORLD!");
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack_string failed"); return(false);}
|
|
|
|
|
|
|
|
rc = ompi_pack (bufC, bufA, 1, OMPI_PACKED);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack failed"); return(false);}
|
|
|
|
rc = ompi_pack (bufC, bufB, 1, OMPI_PACKED);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_pack failed"); return(false);}
|
|
|
|
|
|
|
|
/* we now have a buffer with two strings in it */
|
|
|
|
rc = ompi_unpack_string (bufC, &str1);
|
2004-08-19 03:21:45 +04:00
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_unpack_string failed"); return(false);}
|
2004-08-19 03:18:25 +04:00
|
|
|
|
|
|
|
rc = strcmp ("HELLO ", str1);
|
|
|
|
if (rc) { test_comment ("strcmp returns no zero value."); return (false); }
|
|
|
|
|
|
|
|
rc = ompi_unpack_string (bufC, &str2);
|
2004-08-19 03:21:45 +04:00
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_unpack_string failed"); return(false);}
|
2004-08-19 03:18:25 +04:00
|
|
|
|
|
|
|
rc = strcmp ("WORLD!", str2);
|
|
|
|
if (rc) { test_comment ("strcmp returns no zero value."); return (false); }
|
|
|
|
|
|
|
|
|
|
|
|
rc = ompi_buffer_free (bufA);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_free failed"); return(false);}
|
|
|
|
|
|
|
|
rc = ompi_buffer_free (bufB);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_free failed"); return(false);}
|
|
|
|
|
|
|
|
rc = ompi_buffer_free (bufC);
|
|
|
|
if (OMPI_ERROR==rc) { test_comment ("ompi_buffer_free failed"); return(false);}
|
|
|
|
|
|
|
|
if (str1) { free (str1); }
|
|
|
|
if (str2) { free (str2); }
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2004-08-13 03:07:21 +04:00
|
|
|
/* int dump_buf (ompi_buffer_t buf) */
|
|
|
|
/* { */
|
|
|
|
/* int rc, i, out; */
|
|
|
|
/* */
|
|
|
|
/* rc = 0; */
|
|
|
|
/* i = 0; */
|
|
|
|
/* while (1) { */
|
2004-08-18 18:12:33 +04:00
|
|
|
/* rc = ompi_unpack (buf, &out, 1, OMPI_INT32); */
|
|
|
|
/* if (rc==0) printf("%d[%d] ", i, out); */
|
|
|
|
/* else { */
|
|
|
|
/* printf("\n"); */
|
|
|
|
/* break; */
|
|
|
|
/* } */
|
|
|
|
/* i++; */
|
2004-08-13 03:07:21 +04:00
|
|
|
/* } */
|
|
|
|
/* */
|
|
|
|
/* return (i); */
|
|
|
|
/* } */
|
|
|
|
|