/*
	test join/leave
*/

#include <stdio.h>
#include "pvm3.h"

#define MAXGNAME 32 /* used to be constant, length not important */
#define MAXGTIDS 32 /* used to be constant, Number not important */
#define MAXNGROUPS 32 /* used to be constant, Number not important  */
int
main(argc, argv)
int argc;
char *argv[];
{
	int mytid, ctid[MAXGTIDS];
	char g[MAXGNAME];
	int cc;
	int gid[MAXGTIDS], gs;
	int i;

	mytid = pvm_mytid();
	if (mytid < 0) {
		pvm_perror("th");
		return -1;
		}

	/* join a bunch of groups */
	for (i = 0; i < MAXNGROUPS; i++) {
		sprintf(g, "group%d", i);
		if((gid[i] = pvm_joingroup(g)) < 0) {
			pvm_perror("joining");
			}
		fprintf(stderr, "joined %s\n", g);
		}

	/* test that i'm in the groups */
	for (i = MAXNGROUPS-1; i  >= 0; i--) {
		sprintf(g, "group%d", i);
		if(gid[i] != (cc = pvm_getinst(g, mytid))) {
			fprintf(stderr, "I'm not myself %d %s\n", cc, g);
			pvm_perror("getinst");
			}
		if(mytid != (cc = pvm_gettid(g, gid[i]))) {
			fprintf(stderr, "I'm not myself %d %s\n", cc, g);
			pvm_perror("gettid");
			}
		}

	for (i = 0; i < MAXNGROUPS; i++) {
		sprintf(g, "group%d", i);
		if(pvm_lvgroup(g) < 0)
			pvm_perror("pvm_lvgroup");
		}

	pvm_exit();
}
