Skip to content

Instantly share code, notes, and snippets.

@Apkawa
Created April 17, 2012 13:57
Show Gist options
  • Save Apkawa/2406125 to your computer and use it in GitHub Desktop.
Save Apkawa/2406125 to your computer and use it in GitHub Desktop.
Генератор и чекер тестовых данных
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define MAX_INT 9999999
int randint(int from, int to) {
return (from + rand() % to);
}
int build_data(FILE *file, unsigned long lines) {
unsigned long i;
for (i = 0; i < lines; i++) {
fprintf(file, "%d\n", randint(0, MAX_INT));
}
return 1;
}
int check_data(FILE * file) {
int parent_int = NULL;
int current_int = NULL;
while (fscanf(file, "%d\n", &current_int) != EOF) {
fprintf(stdout, "%d\n", current_int);
if ((parent_int == NULL)|(current_int >= parent_int)) {
parent_int = current_int;
} else {
fprintf(stderr, "Not sortable\n");
break;
}
}
}
int main (int argc, char **argv)
{
int check_data_mode = 0;
char *filename = NULL;
unsigned long lines = 1024;
int index;
int c;
opterr = 0;
while ((c = getopt(argc, argv, "cf:l:")) != -1)
switch (c)
{
case 'c':
check_data_mode = 1;
break;
case 'f':
filename = optarg;
break;
case 'l':
lines = strtol(optarg, NULL, 10);
break;
case '?':
if ((optopt == 'f') | (optopt == 'l'))
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort();
}
printf ("check = %d, filename = %s, lines = %d\n",
check_data_mode, filename, lines);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
FILE * file;
if (filename == NULL)
{
if (check_data_mode) {
file = stdin;
} else {
file = stdout;
}
} else {
if (check_data_mode) {
file = fopen(filename, "r");
} else {
file = fopen(filename, "w");
}
}
if (check_data_mode) {
check_data(file);
} else {
build_data(file, lines);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment