示例#1
0
void
test_wcsrtombs_negative(void)
{
	mbstate_t	ms;
	const wchar_t	*wcs;
	char		mbs[32];
	char		*dst;
	int		e;
	wchar_t		src[32] = BAD_WCS;
	test_t		t;
	int		cnt;

	t = test_start("wcsrtombs_negative");

	(void) memset(&ms, 0, sizeof (ms));
	if (setlocale(LC_ALL, "ru_RU.UTF-8") == NULL) {
		test_failed(t, "setlocale failed: %s", strerror(errno));
	}

	wcs = src;
	dst = mbs;
	cnt = wcsrtombs(dst, &wcs, sizeof (mbs), &ms);
	if (cnt != -1) {
		test_failed(t, "bogus success (%d)", cnt);
	}
	if ((e = errno) != EILSEQ) {
		test_failed(t, "wrong errno, wanted %d (EILSEQ), got %d: %s",
		    EILSEQ, e, strerror(e));
	}
	test_passed(t);
}
示例#2
0
void
test_nl_langinfo_1(const char *locale, struct langinfo_test *test)
{
	char 	tname[128];
	char 	*v;
	test_t	t;

	(void) snprintf(tname, sizeof (tname), "nl_langinfo (locale %s)",
	    locale);
	t = test_start(tname);

	v = setlocale(LC_ALL, locale);
	if (v == NULL) {
		test_failed(t, "setlocale failed: %s", strerror(errno));
	}
	if (strcmp(v, locale) != 0) {
		test_failed(t, "setlocale got %s instead of %s", v, locale);
	}

	for (int i = 0; test[i].value != NULL; i++) {
		v = nl_langinfo(test[i].param);
		test_debugf(t, "%d: expect [%s], got [%s]",
		    test[i].param, test[i].value, v);
		if (strcmp(v, test[i].value) != 0) {
			test_failed(t,
			    "param %d wrong, expected [%s], got [%s]",
			    test[i].param, test[i].value, v);
		}
	}
	test_passed(t);
}
示例#3
0
文件: test.c 项目: unwind/lzjb-stream
static void test_size(size_t size)
{
    char	buf[32];

    void * const encode_end = lzjbstream_size_encode(buf, sizeof buf, size);
    if(encode_end == NULL)
    {
        test_failed("Failed to encode %zu, out of buffer space\n", size);
        return;
    }
    const size_t encoded_length = (char *) encode_end - buf;
    size_t size_out;
    const void * const decode_end = lzjbstream_size_decode(buf, encoded_length, &size_out);
    if(decode_end == NULL)
    {
        test_failed("Failed to decode %zu from %zu bytes of encoded data, ran out of input\n", size, encoded_length);
        return;
    }
    if(size != size_out)
    {
        test_failed("Failed to decode %zu, got %zu instead (from %zu bytes)\n", size, size_out, encoded_length);
        return;
    }
    test_passed();
}
示例#4
0
int
main(int argc, char *argv[])
{
	void *ptr;
 	int n;
	int i;

	for (i = 0; i < 4; i++) {
		ptr = shmem_access(i);
		if (ptr == NULL) {
			test_failed();
		}
	}
	
	int pid = fork();
	if (pid < 0) {
		test_failed();
	}
	else if (pid == 0) {
		for (i = 0; i < 4; i++) {
			n = shmem_count(i);
			if (n != 2) {
				test_failed();
			}
		}
		exit();	
	}
	else {
		wait();
	}

	test_passed();
	exit();
}
示例#5
0
void
test_compile(void)
{
	struct sym_test *st;
	struct compile_env *cenv;
	test_t t;
	int need;

	for (st = next_sym_test(NULL); st; st = next_sym_test(st)) {
		if ((sym != NULL) && strcmp(sym, sym_test_name(st))) {
			continue;
		}
		/* XXX: we really want a sym_test_desc() */
		for (cenv = sym_test_env(st, NULL, &need);
		    cenv != NULL;
		    cenv = sym_test_env(st, cenv, &need)) {
			t = test_start("%s : %c%s", sym_test_name(st),
			    need ? '+' : '-', env_name(cenv));
			if (do_compile(t, st, cenv, need) == 0) {
				test_passed(t);
			}
		}
	}

	if (full_count > 0) {
		test_summary();
	}
}
示例#6
0
/* Test start */
void user_entry()
{
	unsigned i;

	/*
	 * Sine/cosine values on interval -1..1 computed using 10 terms of
	 * Taylor series.
	 */
	for(i = 0; i<ARRAY_LENGTH(ten_terms); ++i) {
		fixed_t sin = sine_fxp(ten_terms[i], 10);
		fixed_t cos = cosine_fxp(ten_terms[i], 10);
		if(sin != sin10[i] || cos != cos10[i])
			test_failed();
	}

	/*
	 * Sine/cosine values on interval -PI..PI computed using 4 terms of
	 * Taylor series.
	 */
	for(i = 0; i<ARRAY_LENGTH(four_terms); ++i) {
		fixed_t sin = sine_fxp(four_terms[i], 4);
		fixed_t cos = cosine_fxp(four_terms[i], 4);
		if(sin != sin4[i] || cos != cos4[i])
			test_failed();
	}

	test_passed();
}
示例#7
0
文件: write.c 项目: squallee/CS537
int
main(int argc, char *argv[])
{
  int fd;
  char buf[MAX];
  char buf2[MAX];
  int n;
  int i;
  
  for(i = 0; i < MAX; i++){
    buf[i] = (char)(i+(int)'0');
  }
  
  if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE | O_RDWR)) < 0){
    printf(1, "Failed to create the small file\n");
    test_failed();
    exit();
  }
  
  if((n = write(fd, buf, MAX)) < 0){
    printf(1, "Write failed!\n");
    test_failed();
  }
  printf(1, "bytes written = %d\n", n);
  close(fd);
  
  if(n != (NDIRECT+1)*4){
    printf(1, "Failed to write the right amount to the small file.\n");
    test_failed();
  }
  
  
  if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE | O_RDWR)) < 0){
    printf(1, "Failed to create the small file\n");
    exit();
  }
  
  if((n = read(fd, buf2, MAX)) < 0){
    printf(1, "Read failed!\n");
    exit();
  }
  printf(1, "bytes read = %d\n", n);
  close(fd);
  
  if(n != (NDIRECT+1)*4){
    printf(1, "Failed to read the right amount to the small file.\n");
    test_failed();
  }
  
  for(i = 0; i < (NDIRECT+1)*4; i++){
    if(buf[i] != buf2[i]){
      printf(1, "Data mismatch.\n");
      test_failed();
    }
  }
  
  test_passed();
  exit();
}
示例#8
0
文件: main.c 项目: jkenergy/scalios
int main()
{
	do_test();
#ifdef REINIT_FLAG
	do_test();
#endif
	test_passed();
}
示例#9
0
文件: testp3.c 项目: subasreev/cs537
int
main(int argc, char *argv[])
{
  char *ptr;
  int i;
  
  ptr = shmem_access(3);
  if (ptr == NULL) {
    test_failed();
  }
  
  char arr[4] = "tmp";
  for (i = 0; i < 4; i++) {
    *(ptr+i) = arr[i];
  }
  
  //argstr
  int fd = open(ptr, O_WRONLY|O_CREATE);
  if (fd == -1) {
    printf(1, "open system call failed to take a string from within a shared page\n");
    test_failed();
  }
  
  //argptr
  int n = write(fd, ptr, 10);
  if (n == -1) {
    printf(1, "write system call failed to take a pointer from within a shared page\n");
    test_failed();
  }
  
  //making sure invalid strings are still caught
  int fd2 = open((char *)(USERTOP/2), O_WRONLY|O_CREATE);
  if (fd2 != -1) {
    printf(1, "open system call successfully accepted an invalid string\n");
    test_failed();
  }
  
  //making sure invalid pointers are still caught
  n = write(fd, (char *)(USERTOP/2), 10);
  if (n != -1) {
    printf(1, "write system call successfully accepted an invalid pointer\n");
    test_failed();
  }
  
  //making sure edge case is checked
  printf(1, "%d\n", ptr);
  n = write(fd, (char *)(ptr + 4094), 3);
  if (n != -1) {
    printf(1, "write system call successfully accepted an overflowing pointer in a shared page\n");
    test_failed();
  }
  
  test_passed();
  exit();
}
示例#10
0
int
main(int argc, char *argv[])
{	
	char *ptr;
	int i;
	char d, arr[6] = "CS537";
	

	int pid = fork();
	if (pid < 0) {
		test_failed();
	}	
	else if (pid == 0) {
		// printf(1, "child shmem_counter before access: %d\n", shmem_count(3));

		ptr = shmem_access(3);
		if (ptr == NULL) {
			test_failed();
		}
		
		for (i = 0; i < 5; i++) {
			*(ptr+i) = arr[i];
			printf(1, "write: %c\n", *(ptr+i));
		}
		for (i = 0; i < 5; i++) {
			d = *(ptr+i);
			printf(1, "read in child: %c\n", d);
		}
		// printf(1, "child shmem_counter after access: %d\n", shmem_count(3));

		exit();
	}
	else {
		wait();
		// printf(1, "parent shmem_counter before access: %d\n", shmem_count(3));		
		ptr = shmem_access(3);
		// printf(1, "parent shmem_counter after access: %d\n", shmem_count(3));		

		if (ptr == NULL) {
			test_failed();
		}
		
		for (i = 0; i < 5; i++) {		
			printf(1, "read in parent: %c\n", *(ptr+i));
			if (*(ptr+i) != arr[i]) {
				printf(1, "%d\t%c\t%c\n", shmem_count(3), *(ptr+i), arr[i]);
				test_failed();
			}
		}
	}
	
	test_passed();
	exit();
}
示例#11
0
void
test_wcsrtombs_thr_work(test_t t, void *arg)
{
	_NOTE(ARGUNUSED(arg));
	for (int j = 0; j < NUMITR; j++) {
		test_debugf(t, "iteration %d", j);
		for (int i = 0; locales[i].locale != NULL; i++) {
			test_wcsrtombs_thr_iter(t, locales[i].locale,
			    locales[i].test);
		}
	}
	test_passed(t);
}
示例#12
0
void
test_wcsrtombs_partial(void)
{
	test_t		t;
	mbstate_t	ms;
	wchar_t		src[32] = HELLO_RU_WCS;
	char		mbs[32];
	char		*dst;
	const wchar_t	*wcs;
	size_t 		cnt;


	(void) memset(&ms, 0, sizeof (ms));
	t = test_start("wcsrtombs_partial");

	if (setlocale(LC_ALL, "ru_RU.UTF-8") == NULL) {
		test_failed(t, "setlocale failed: %s", strerror(errno));
	}

	wcs = src;
	dst = mbs;
	cnt = wcsrtombs(dst, &wcs, 1, &ms);
	if (cnt != 0) {
		test_failed(t, "gave back a conversion cnt %d != 0", cnt);
	}
	if (wcs != src) {
		test_failed(t, "incorrectly advanced wcs");
	}

	cnt = wcsrtombs(dst, &wcs, 2, &ms);
	if (cnt != 2) {
		test_failed(t, "gave back a conversion cnt %d != 2", cnt);
	}
	dst += cnt;

	cnt = wcsrtombs(dst, &wcs, 4, &ms);
	dst += cnt;

	cnt = wcsrtombs(dst, &wcs, sizeof (mbs) - strlen(mbs), &ms);
	if (extra_debug) {
		test_debugf(t, "mbs is %s", mbs);
	}
	if (strcmp(mbs, HELLO_RU_MBS) != 0) {
		test_failed(t, "wrong result: %s != %s", mbs, HELLO_RU_MBS);
	}
	test_passed(t);
}
示例#13
0
文件: write2.c 项目: lewiskunik/p5b
int
main(int argc, char *argv[])
{
  int fd;
  char buf[NITERATIONS];
  char result; //character read from file
  int n;
  int i;
  
  for(i = 0; i < NITERATIONS; i++){
	printf(1, "writing %d\n", (i+(int)'0'));
    buf[i] = (char)(i+(int)'0');
  }
  
  //open, write 1 byte, close
  for(i = 0; i < NITERATIONS; i++){
    if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE | O_RDWR)) < 0){
      printf(1, "Failed to create the small file\n");
      test_failed();
    }
    if((n = write(fd, &buf[i], 1)) != 1){
      printf(1, "Write failed!\n");
      test_failed();
    }
    close(fd);
  }
  
  //read
  if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE | O_RDWR)) < 0){
    printf(1, "Failed to open the small file\n");
    test_failed();
  }
  if((n = read(fd, &result, 10)) != 1){
    printf(1, "Read failed! %d\n", n);
    test_failed();
  }
  close(fd);
  printf(1, "returned %c\n", result);
  
  if(result != buf[NITERATIONS-1]){
    printf(1, "Data mismatch.\n");
    test_failed();
  }

  test_passed();
	exit();
}
示例#14
0
文件: read2.c 项目: squallee/CS537
int
main(int argc, char *argv[])
{
    int fd;
    char buf[MAX];
    char buf2[MAX];
    int n;
    int i;

    for(i = 0; i < MAX; i++) {
        buf[i] = (char)(i+(int)'0');
    }
    memset(buf2, 0, MAX);

    //create and write
    if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE | O_RDWR)) < 0) {
        printf(1, "Failed to create the small file\n");
        test_failed();
        exit();
    }
    if((n = write(fd, buf, MAX)) != MAX) {
        printf(1, "Write failed!\n");
        test_failed();
    }
    close(fd);

    //read
    if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE | O_RDWR)) < 0) {
        printf(1, "Failed to open the small file\n");
        test_failed();
    }
    for(i = 0; i < MAX; i++) {
        if((n = read(fd, &buf2[i], 1)) != 1) {
            printf(1, "Read failed!\n");
            test_failed();
        }

        if(buf[i] != buf2[i]) {
            printf(1, "Data mismatch.\n");
            test_failed();
        }
    }
    close(fd);

    test_passed();
    exit();
}
示例#15
0
void
test_wcsrtombs_l(const char *locale, struct wcsrtombs_test *test)
{
	test_t	t;
	locale_t loc;
	char 	*v;
	mbstate_t	ms;

	t = test_start("wcsrtombs_l (locale %s)", locale);

	v = setlocale(LC_ALL, "C");
	if (v == NULL) {
		test_failed(t, "setlocale failed: %s", strerror(errno));
	}
	if (strcmp(v, "C") != 0) {
		test_failed(t, "setlocale got %s instead of %s", v, "C");
	}

	loc = newlocale(LC_ALL_MASK, locale, NULL);
	if (loc == NULL) {
		test_failed(t, "newlocale failed: %s", strerror(errno));
	}

	for (int i = 0; test[i].mbs[0] != 0; i++) {
		char mbs[32];
		const wchar_t *wcs = test[i].wcs;
		size_t cnt;

		(void) memset(&ms, 0, sizeof (ms));
		(void) memset(mbs, 0, sizeof (mbs));
		cnt = wcsrtombs_l(mbs, &wcs, sizeof (mbs), &ms, loc);
		if (cnt != strlen(test[i].mbs)) {
			test_failed(t, "incorrect return value: %d != %d",
			    cnt, strlen(test[i].mbs));
		}
		if (strcmp(mbs, test[i].mbs) != 0) {
			test_failed(t, "wrong result: %s != %s", mbs,
			    test[i].mbs);
		}
		if (extra_debug) {
			test_debugf(t, "mbs is %s", mbs);
		}
	}
	test_passed(t);
}
示例#16
0
文件: persistent.c 项目: kirthan18/OS
int
main(int argc, char *argv[])
{	
	char *ptr;
	int i;
	char arr[6] = "CS537";
	

	int pid = fork();
	if (pid < 0) {
		test_failed();
	}	
	else if (pid == 0) {
		ptr = shmem_access(3);
		if (ptr == NULL) {
			test_failed();
		}
		
		for (i = 0; i < 5; i++) {
			*(ptr+i) = arr[i];
		}

		exit();
	}
	else {
		wait();
		
		ptr = shmem_access(3);
		if (ptr == NULL) {
			test_failed();
		}
		
		for (i = 0; i < 5; i++) {		
			if (*(ptr+i) != arr[i]) {
				test_failed();
			}
		}
	}
	
	test_passed();
	exit();
}
示例#17
0
/* Test start */
void user_entry()
{
	int i;

	/* Fill source block with pattern */
	for(i=0; i<BLOCK_SZ; ++i)
		src_block[i] = i % 256;

	/* Copy block */
	memcpy(dst_block, src_block, BLOCK_SZ);

	/* Check blocks content */
	for(i=0; i<BLOCK_SZ; ++i) {
		char pat = i % 256;
		if(src_block[i] != pat || dst_block[i] != pat)
			test_failed();
	}

	test_passed();
}
示例#18
0
/* Standard shutdown hook
 * 
 * $Req: artf1227 $
 */
void ShutdownHook(StatusType error)
{
#ifdef USESHUTDOWNHOOK
	SET_TESTEVENT("ShutdownHook");
	/* Check that the status passed to ShutdownOS() gets passed to this hook.
	 * 
	 * $Req: artf1218 $
	 */
	if(test_shutdown_code && error != shutdown_code) {
		test_failed(OS_HERE);
	}
	/* Check that the system shuts down if a stack overflow detected (when expected).
	 * 
	 * $Req: artf1040 $
	 */ 
	if(test_shutdown_code && error == shutdown_code && error == E_OS_STACKFAULT) {
		test_finished();
		test_passed();
	}
#else
	test_failed(OS_HERE);
#endif
}
示例#19
0
文件: test.c 项目: unwind/lzjb-stream
static void test_decompress(void)
{
    /* Here's one I made earlier. */
    const uint8_t data[] = { 0x0, 0x5b, 0x27, 0x4c, 0x45, 0x4d, 0x50, 0x45, 0x4c, 0x0, 0x5f, 0x53, 0x49, 0x5a,
                             0x45, 0x27, 0x2c, 0x20, 0x0, 0x27, 0x4d, 0x41, 0x54, 0x43, 0x48, 0x5f, 0x42, 0x88, 0x49, 0x54, 0x53,
                             0x1c, 0xe, 0x4d, 0x41, 0x58, 0x20, 0xd, 0x84, 0x49, 0x4e, 0x1c, 0xd, 0x52, 0x41, 0x4e, 0x47, 0x8, 0x37,
                             0x10, 0x4e, 0x42, 0x42, 0x59, 0x4, 0x17, 0x4f, 0x46, 0x46, 0x0, 0x53, 0x45, 0x54, 0x5f, 0x4d, 0x41,
                             0x53, 0x4b, 0x1, 0x4, 0xf, 0x5f, 0x5f, 0x62, 0x75, 0x69, 0x6c, 0x74, 0x20, 0x69, 0x6e, 0x73, 0x5f,
                             0x5f, 0xc, 0x10, 0x64, 0x6f, 0x42, 0x63, 0x14, 0xb, 0x66, 0x69, 0x6c, 0x65, 0x14, 0xc, 0x6e, 0x4,
                             0x61, 0x6d, 0x18, 0xc, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x0, 0x67, 0x65, 0x5f, 0x5f, 0x27, 0x2c, 0x20,
                             0x27, 0x0, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x0, 0x27, 0x2c, 0x20, 0x27, 0x64, 0x65,
                             0x63, 0x6f, 0x0, 0x64, 0x65, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x27, 0x0, 0x2c, 0x20, 0x27, 0x64, 0x65,
                             0x63, 0x6f, 0x6d, 0x0, 0x70, 0x72, 0x65, 0x73, 0x73, 0x27, 0x2c, 0x20, 0x0, 0x27, 0x65, 0x6e, 0x63,
                             0x6f, 0x64, 0x65, 0x5f, 0x0, 0x73, 0x69, 0x7a, 0x65, 0x27, 0x5d
                           };
    const char test_original[] = "['LEMPEL_SIZE', 'MATCH_BITS', 'MATCH_MAX', 'MATCH_MIN', 'MATCH_RANGE', 'NBBY', 'OFFSET_MASK', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'compress', 'decode_size', 'decompress', 'encode_size']";
    const size_t test_original_len = strlen(test_original);
    uint8_t tmp1[sizeof test_original] = { 0 }, tmp2[sizeof test_original] = { 0 }, tmp3[sizeof test_original] = { 0 };
    LZJBStream stream;
    size_t i;

    /* First, decompress all at once. */
    lzjbstream_init_memory(&stream, tmp1, test_original_len);
    lzjbstream_decompress(&stream, data, sizeof data);
    const size_t len1 = strlen(tmp1);
    if(len1 == test_original_len && strcmp(tmp1, test_original) == 0)
        test_passed();
    else
        test_failed("Failed full-file streaming decompression, resulting len=%zu (expected %zu)", len1, sizeof test_original);

    /* Second, decompress a single byte (!) at a time. Mean. */
    lzjbstream_init_memory(&stream, tmp2, test_original_len);
    for(i = 0; i < sizeof data; ++i)
    {
        const uint8_t	tmp = data[i];	/* Decompress ultra-slowly, feeding only a single byte at a time. */
        if(!lzjbstream_decompress(&stream, &tmp, 1))
            break;
    }
    const size_t len2 = strlen(tmp2);
    if(len2 == test_original_len && strcmp(tmp2, test_original) == 0)
        test_passed();
    else
        test_failed("Failed 1-byte streaming decompression, resulting len=%zu (expected %zu)", len2, sizeof test_original);

    /* Third, decompress a random amount of bytes at a time. */
    lzjbstream_init_memory(&stream, tmp3, test_original_len);
    size_t pos = 0;
    bool busy = true;
    while(busy)
    {
        int chunk = (rand() % 19 + 1);
        if(pos + chunk > sizeof data)
            chunk = sizeof data - pos;
        busy = lzjbstream_decompress(&stream, data + pos, chunk);
        pos += chunk;
    }
    const size_t len3 = strlen(tmp3);
    if(len3 == test_original_len && strcmp(tmp3, test_original) == 0)
        test_passed();
    else
        test_failed("Failed random-chunked streaming decompression, resulting len=%zu (expected %zu)", len3, sizeof test_original);
}
示例#20
0
文件: write3.c 项目: squallee/CS537
int
main(int argc, char *argv[])
{
  int fd, i;
  char buf[SIZE];
  char buf2[SIZE];
  char tmp;
  struct stat st;
  
  for(i = 0; i < SIZE; i++){
    buf[i] = (char)(i+(int)'0');
  }
  memset(buf2, 0, SIZE);
  
  //open, write 1 byte to the end, close
  for(i = 0; i < SIZE+5; i++){
    if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE | O_RDWR)) < 0){
      printf(1, "Failed to create the small file\n");
      test_failed();
    }
    
    while(read(fd, &tmp, 1) == 1) { } //go to end of file
    
    if(write(fd, &buf[i], 1) != 1){
      break;
    }
    close(fd);
  }
  
  //read
  if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE | O_RDWR)) < 0){
    printf(1, "Failed to open the small file\n");
    test_failed();
  }
  
  if(fstat(fd, &st) < 0){
    printf(1, "Failed to get stat on the small file\n");
    test_failed();
  }
  
  if(st.size != SIZE){
    printf(1, "Invalid file size.\n");
    test_failed();
  }
  
  if(read(fd, buf2, SIZE) != SIZE){
    printf(1, "Read failed!\n");
    test_failed();
  }
  close(fd);
  
  for(i = 0; i < SIZE; i++){
    if(buf[i] != buf2[i]){
      printf(1, "Data mismatch.\n");
      test_failed();
    }
  }
  
  test_passed();
	exit();
}
示例#21
0
int
main(int argc, char *argv[])
{
  int fd, i;
  char buf[MAX]; //data out
  char buf2[MAX]; //data in
  char buf3[MAX]; //data in
  
  char buf_unchanged[MAX]; //this data should never be changed throughout this program once initialized

  for(i = 0; i < MAX; i++){
    buf[i] = (char)(i+(int)'0');
  }
  
  for(i = 0; i < MAX; i++){
    buf_unchanged[i] = (char)(i-(int)'0');
  }
  
  //create
  if((fd = open("test_file.txt", O_CREATE | O_SMALLFILE)) < 0){
    printf(1, "Failed to create a small file\n");
    test_failed();
  }
  close(fd);
  
  
  //write
  if((fd = open("test_file.txt", O_RDWR)) < 0){
    printf(1, "Failed to open a small file\n");
    test_failed();
  }
 
  if(write(fd, buf, MAX) != MAX){
    printf(1, "Write failed!\n");
    test_failed();
  }
  close(fd);
  
  
  //read to make sure normal write succeeded
  if((fd = open("test_file.txt", O_RDONLY)) < 0){
    printf(1, "Failed to open a small file as read only\n");
    test_failed();
  }
  
  if(read(fd, buf2, MAX) != MAX){
    printf(1, "Read failed!\n");
    test_failed();
  }
  close(fd);
  
  for(i = 0; i < MAX; i++){
    if(buf[i] != buf2[i]){
      printf(1, "Data mismatch\n");
      test_failed();
    }
  }
  
  
  //read with O_WRONLY flag
  if((fd = open("test_file.txt", O_WRONLY)) < 0){
    printf(1, "Failed to open a small file as write only\n");
    test_failed();
  }
  
  if(read(fd, buf_unchanged, MAX) >= 0){ //MAIN TEST
    printf(1, "Read succeeded despite O_WRONLY flag!\n");
    test_failed();
  }
  close(fd);
  
  for(i = 0; i < MAX; i++){
    if(buf_unchanged[i] != (char)(i-(int)'0')){
      printf(1, "Failed read system call modified buffer passed in!\n");
      test_failed();
    }
  }
  
  
  //read to make sure normal write is still persisted correctly, in case read with O_WRONLY somehow corrupted data
  if((fd = open("test_file.txt", O_RDONLY)) < 0){
    printf(1, "Failed to open a small file as read only\n");
    test_failed();
  }
  
  if(read(fd, buf3, MAX) != MAX){
    printf(1, "Read failed!\n");
    test_failed();
  }
  close(fd);
  
  for(i = 0; i < MAX; i++){
    if(buf[i] != buf3[i]){
      printf(1, "Data mismatch\n");
      test_failed();
    }
  }
  
  test_passed();
	exit();
}
示例#22
0
void
find_compiler(void)
{
	test_t t;
	int i;
	FILE *cf;

	t = test_start("finding compiler");

	if ((cf = fopen(cfile, "w+")) == NULL) {
		test_failed(t, "Unable to open %s for write: %s", cfile,
		    strerror(errno));
		return;
	}
	(void) fprintf(cf, "#include <stdio.h>\n");
	(void) fprintf(cf, "int main(int argc, char **argv) {\n");
	(void) fprintf(cf, "#if defined(__SUNPRO_C)\n");
	(void) fprintf(cf, "exit(51);\n");
	(void) fprintf(cf, "#elif defined(__GNUC__)\n");
	(void) fprintf(cf, "exit(52);\n");
	(void) fprintf(cf, "#else\n");
	(void) fprintf(cf, "exit(99)\n");
	(void) fprintf(cf, "#endif\n}\n");
	(void) fclose(cf);

	for (i = 0; compilers[i] != NULL; i++) {
		char cmd[256];
		int rv;

		(void) snprintf(cmd, sizeof (cmd),
		    "%s %s %s -o %s >/dev/null 2>&1",
		    compilers[i], MFLAG, cfile, efile);
		test_debugf(t, "trying %s", cmd);
		rv = system(cmd);

		test_debugf(t, "result: %d", rv);

		if ((rv < 0) || !WIFEXITED(rv) || WEXITSTATUS(rv) != 0)
			continue;

		rv = system(efile);
		if (rv >= 0 && WIFEXITED(rv)) {
			rv = WEXITSTATUS(rv);
		} else {
			rv = -1;
		}

		switch (rv) {
		case 51:	/* STUDIO */
			test_debugf(t, "Found Studio C");
			c89flags = "-Xc -errwarn=%all -v -xc99=%none " MFLAG;
			c99flags = "-Xc -errwarn=%all -v -xc99=%all " MFLAG;
			c11flags = NULL;
			if (extra_debug) {
				test_debugf(t, "c89flags: %s", c89flags);
				test_debugf(t, "c99flags: %s", c99flags);
			}
			test_passed(t);
			break;
		case 52:	/* GCC */
			test_debugf(t, "Found GNU C");
			c89flags = "-Wall -Werror -std=c89 -nostdinc "
			    "-isystem /usr/include " MFLAG;
			c99flags = "-Wall -Werror -std=c99 -nostdinc "
			    "-isystem /usr/include " MFLAG;
			c11flags = "-Wall -Werror -std=c11 -nostdinc "
			    "-isystem /usr/include " MFLAG;
			if (extra_debug) {
				test_debugf(t, "c89flags: %s", c89flags);
				test_debugf(t, "c99flags: %s", c99flags);
			}
			test_passed(t);
			break;
		case 99:
			test_debugf(t, "Found unknown (unsupported) compiler");
			continue;
		default:
			continue;
		}
		myasprintf(&compiler, "%s", compilers[i]);
		test_debugf(t, "compiler: %s", compiler);
		return;
	}
	test_failed(t, "No compiler found.");
}