示例#1
0
int
main(int argc, char *argv[]) {
  if (dataset_init(argc, argv) == -1) {
    dataset_usage();
    return EXIT_FAILURE;
  }

  int size;
  int *buf = malloc(SSORT_MAX_NUMS * sizeof(int));
  if (buf == NULL) {
    pexit(progname);
  }

  size = dataset_get(buf, SSORT_MAX_NUMS);
  if (size == -1) {
    pexit(progname);
  }

  selection_sort(buf, size);
  dataset_print(buf, size);

  free(buf);

  return EXIT_SUCCESS;
}
示例#2
0
static int read_datafile (const char *fname, dataset *dset)
{
#ifdef PLAY_AUTOFIT_LINE
    int fitline = 0;
#endif
    char line[256];
    int i, err = 0;
    int got_e = 0, y2data = 0;
    FILE *fdat;

    dataset_init(dset);

    fdat = gretl_fopen(fname, "r");
    if (fdat == NULL) {
	fprintf(stderr, "Couldn't open '%s'\n", fname);
	return 1;
    } else {
	fprintf(stderr, "Reading %s...\n", fname);
    }

    while (fgets(line, sizeof line, fdat)) {
	tailstrip(line);
	if (get_comment(line, dset)) {
	    continue;
	} else if (!strcmp(line, "e")) {
	    fprintf(stderr, "Got end of data marker\n");
	    got_e++;
	    if (got_e == 2) {
		/* can't handle more than two series! */
		break;
	    }
	} else if (strstr(line, "automatic fitted")) {
#ifdef PLAY_AUTOFIT_LINE
	    fitline = 1;
#endif
	    continue;
	} else if (isdigit((unsigned char) line[0])) {
	    if (strstr(line, "title")) {
#ifdef PLAY_AUTOFIT_LINE
		if (fitline) {
		    get_fit_params(line, dset);
		}
#endif
		continue;
	    }
	    if (!got_e) {
		dset->n += 1;
	    } else if (!y2data) {
		y2data = 1;
	    }
	}
    }

    if (dset->n == 0) {
	fprintf(stderr, "No data in '%s'\n", fname);
	err = 1;
	goto bailout;
    } 

    dset->points = malloc(dset->n * sizeof *dset->points);
    if (dset->points == NULL) {
	err = 1;
	fputs("Out of memory\n", stderr);
	goto bailout;
    }

    if (y2data) {
	dset->y2 = malloc(dset->n * sizeof *dset->y2);
	if (dset->y2 == NULL) {
	    err = 1;
	    fputs("Out of memory\n", stderr);
	    goto bailout;
	}
	dset->series2 = 1;
    }

    rewind(fdat);

    i = got_e = 0;
    while (!err && fgets(line, 256, fdat)) {
	tailstrip(line);
	if (!strcmp(line, "e")) {
	    got_e++;
	    if (got_e == 2) {
		break;
	    } 
	    i = 0;
	} else if (isdigit((unsigned char) line[0])) {
	    double x, y;

	    if (strstr(line, "title")) {
		continue;
	    }

	    if (get_data_x_y(line, &x, &y)) {
		fprintf(stderr, "Couldn't read data on line %d\n", i + 1);
		err = 1;
	    } else {
		if (!got_e) {
		    dset->points[i].x = x;
		    dset->points[i].y = y;
		} else {
		    dset->y2[i] = y;
		}
	    }
	    i++;
	}
    }    

 bailout:

    fclose(fdat);

    if (err) {
	dataset_free(dset);
    }

    return err;
}
示例#3
0
文件: ds_olist.c 项目: gwtony/wsocket
int main(int argc, char *argv[]) {
	int r;
	int max=1000000,count=50000, i;

//	max = atoi(argv[1]);
//	count = atoi(argv[2]);

	srand(getpid());
	printf("### Function test ###\n");
	printf("Args max:%d length:%d\n", max, count);

	dataset_init(count);

	printf("=== Init olist_ ===\n");
	ol = olist_new(max, compare_data);
	//olist_show(ol);

	printf("=== Insert olist_ ===\n");
	fill_ol();
	//olist_show(ol);

	printf("=== olist remove entry ===\n");
	for (i = 0; i < dataset_size; i++) {
		printf("remove %d\n", i);
		if (olist_remove_entry_by_datap(ol, dataset+i)) {
			olist_show(ol);
			abort();
		}
	}
	assert(ol->length==0);

	int *datap;

	printf("=== Fetch head olist ===\n");
	fill_ol();
	for (i=0;i<dataset_size;++i) {
		datap = olist_fetch_head(ol);
		if (*datap != dataset_o[i]) {
			printf("fetch %d error: Got %d while expecting %d\n", i, *datap, dataset_o[i]);
		}
	}
	assert(ol->length == 0);

	printf("=== Peek head olist ===\n");
	fill_ol();
	datap = olist_peek_head(ol);
	assert(*datap==dataset_o[0]);
	assert(ol->length == dataset_size);
	olist_add_entry(ol, datap);

	printf("=== olist find entry ===\n");
	for (i=0;i<dataset_size;++i) {
		assert(olist_if_exists(ol, olist_peek_head(ol))==0);
	}

#define	NR_THR	24
	printf("=== Loop test in %d threads ===\n", NR_THR);
	pthread_t tid[NR_THR];
	for (i=0;i<NR_THR;++i) {
		pthread_create(tid+i, NULL, thr_loop, (void*)i);
	}
	printf("%d threads created.\n", i);
	for (i=0;i<NR_THR;++i) {
		pthread_join(tid[i], NULL);
	}
	printf("%d threads joined.\n", i);

	printf("=== Remove node test 2 ===\n");
	int same[10000];
	memset(same, 0, sizeof(same));
	olist_destroy(ol);

	ol = olist_new(1000000, compare_data);
	fill_ol();
	for (i=0;i<10000;++i) {
		assert(olist_add_entry(ol, same+i)==0);
	}
	for (i=0;i<10000;++i) {
		assert(olist_remove_entry_by_datap(ol, same+i)==0);
	}

	printf("=== Delete olist ===\n");
	olist_destroy(ol);
	ol = NULL;

	return 0;
}