fmemopen2_test.c (277446) | fmemopen2_test.c (291840) |
---|---|
1/*- 2Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org> 3 4Redistribution and use in source and binary forms, with or without 5modification, are permitted provided that the following conditions 6are met: 71. Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. --- 15 unchanged lines hidden (view full) --- 24*/ 25 26/* 27 * Test basic FILE * functions (fread, fwrite, fseek, fclose) against 28 * a FILE * retrieved using fmemopen() 29 */ 30 31#include <sys/cdefs.h> | 1/*- 2Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org> 3 4Redistribution and use in source and binary forms, with or without 5modification, are permitted provided that the following conditions 6are met: 71. Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. --- 15 unchanged lines hidden (view full) --- 24*/ 25 26/* 27 * Test basic FILE * functions (fread, fwrite, fseek, fclose) against 28 * a FILE * retrieved using fmemopen() 29 */ 30 31#include <sys/cdefs.h> |
32__FBSDID("$FreeBSD: stable/10/lib/libc/tests/stdio/fmemopen2_test.c 277446 2015-01-20 21:59:48Z ngie $"); | 32__FBSDID("$FreeBSD: stable/10/lib/libc/tests/stdio/fmemopen2_test.c 291840 2015-12-05 04:17:40Z ngie $"); |
33 34#include <errno.h> 35#include <stdio.h> 36#include <string.h> 37#include <strings.h> | 33 34#include <errno.h> 35#include <stdio.h> 36#include <string.h> 37#include <strings.h> |
38 |
|
38#include <atf-c.h> 39 40ATF_TC_WITHOUT_HEAD(test_preexisting); 41ATF_TC_BODY(test_preexisting, tc) 42{ | 39#include <atf-c.h> 40 41ATF_TC_WITHOUT_HEAD(test_preexisting); 42ATF_TC_BODY(test_preexisting, tc) 43{ |
43 /* 44 * Use a pre-existing buffer. 45 */ 46 | 44 /* Use a pre-existing buffer. */ |
47 char buf[512]; 48 char buf2[512]; 49 char str[] = "Test writing some stuff"; 50 char str2[] = "AAAAAAAAA"; 51 char str3[] = "AAAA writing some stuff"; 52 FILE *fp; 53 size_t nofw, nofr; 54 int rc; --- 14 unchanged lines hidden (view full) --- 69 fp = fmemopen(buf, sizeof(buf), "r"); 70 ATF_REQUIRE(fp != NULL); 71 72 /* Read from the buffer. */ 73 bzero(buf2, sizeof(buf2)); 74 nofr = fread(buf2, 1, sizeof(buf2), fp); 75 ATF_REQUIRE(nofr == sizeof(buf2)); 76 | 45 char buf[512]; 46 char buf2[512]; 47 char str[] = "Test writing some stuff"; 48 char str2[] = "AAAAAAAAA"; 49 char str3[] = "AAAA writing some stuff"; 50 FILE *fp; 51 size_t nofw, nofr; 52 int rc; --- 14 unchanged lines hidden (view full) --- 67 fp = fmemopen(buf, sizeof(buf), "r"); 68 ATF_REQUIRE(fp != NULL); 69 70 /* Read from the buffer. */ 71 bzero(buf2, sizeof(buf2)); 72 nofr = fread(buf2, 1, sizeof(buf2), fp); 73 ATF_REQUIRE(nofr == sizeof(buf2)); 74 |
77 /* | 75 /* |
78 * Since a write on a FILE * retrieved by fmemopen 79 * will add a '\0' (if there's space), we can check 80 * the strings for equality. 81 */ 82 ATF_REQUIRE(strcmp(str, buf2) == 0); 83 84 /* Close the FILE *. */ 85 rc = fclose(fp); --- 14 unchanged lines hidden (view full) --- 100 101 /* Check that the string was not modified after the first 4 bytes. */ 102 ATF_REQUIRE(strcmp(str, str3) == 0); 103} 104 105ATF_TC_WITHOUT_HEAD(test_autoalloc); 106ATF_TC_BODY(test_autoalloc, tc) 107{ | 76 * Since a write on a FILE * retrieved by fmemopen 77 * will add a '\0' (if there's space), we can check 78 * the strings for equality. 79 */ 80 ATF_REQUIRE(strcmp(str, buf2) == 0); 81 82 /* Close the FILE *. */ 83 rc = fclose(fp); --- 14 unchanged lines hidden (view full) --- 98 99 /* Check that the string was not modified after the first 4 bytes. */ 100 ATF_REQUIRE(strcmp(str, str3) == 0); 101} 102 103ATF_TC_WITHOUT_HEAD(test_autoalloc); 104ATF_TC_BODY(test_autoalloc, tc) 105{ |
108 /* 109 * Let fmemopen allocate the buffer. 110 */ 111 | 106 /* Let fmemopen allocate the buffer. */ |
112 char str[] = "A quick test"; 113 FILE *fp; 114 long pos; 115 size_t nofw, nofr, i; 116 int rc; 117 118 /* Open a FILE * using fmemopen. */ 119 fp = fmemopen(NULL, 512, "w+"); --- 4 unchanged lines hidden (view full) --- 124 nofw = fwrite("a", 1, 1, fp); 125 ATF_REQUIRE(nofw == 1); 126 } 127 128 /* Get the current position into the stream. */ 129 pos = ftell(fp); 130 ATF_REQUIRE(pos == 512); 131 | 107 char str[] = "A quick test"; 108 FILE *fp; 109 long pos; 110 size_t nofw, nofr, i; 111 int rc; 112 113 /* Open a FILE * using fmemopen. */ 114 fp = fmemopen(NULL, 512, "w+"); --- 4 unchanged lines hidden (view full) --- 119 nofw = fwrite("a", 1, 1, fp); 120 ATF_REQUIRE(nofw == 1); 121 } 122 123 /* Get the current position into the stream. */ 124 pos = ftell(fp); 125 ATF_REQUIRE(pos == 512); 126 |
132 /* 133 * Try to write past the end, we should get a short object count (0) 134 */ | 127 /* Try to write past the end, we should get a short object count (0) */ |
135 nofw = fwrite("a", 1, 1, fp); 136 ATF_REQUIRE(nofw == 0); 137 138 /* Close the FILE *. */ 139 rc = fclose(fp); 140 ATF_REQUIRE(rc == 0); 141 142 /* Open a FILE * using a wrong mode */ --- 24 unchanged lines hidden (view full) --- 167 /* Open a FILE * for updating our buffer. */ 168 fp = fmemopen(buf, sizeof(buf), "w+"); 169 ATF_REQUIRE(fp != NULL); 170 171 /* Write our string into the buffer. */ 172 nofw = fwrite(str, 1, sizeof(str), fp); 173 ATF_REQUIRE(nofw == sizeof(str)); 174 | 128 nofw = fwrite("a", 1, 1, fp); 129 ATF_REQUIRE(nofw == 0); 130 131 /* Close the FILE *. */ 132 rc = fclose(fp); 133 ATF_REQUIRE(rc == 0); 134 135 /* Open a FILE * using a wrong mode */ --- 24 unchanged lines hidden (view full) --- 160 /* Open a FILE * for updating our buffer. */ 161 fp = fmemopen(buf, sizeof(buf), "w+"); 162 ATF_REQUIRE(fp != NULL); 163 164 /* Write our string into the buffer. */ 165 nofw = fwrite(str, 1, sizeof(str), fp); 166 ATF_REQUIRE(nofw == sizeof(str)); 167 |
175 /* 176 * Now seek to the end and check that ftell 177 * gives us sizeof(str). 178 */ | 168 /* Now seek to the end and check that ftell gives us sizeof(str). */ |
179 rc = fseek(fp, 0, SEEK_END); 180 ATF_REQUIRE(rc == 0); 181 pos = ftell(fp); 182 ATF_REQUIRE(pos == sizeof(str)); 183 184 /* Close the FILE *. */ 185 rc = fclose(fp); 186 ATF_REQUIRE(rc == 0); --- 71 unchanged lines hidden (view full) --- 258 */ 259 260 FILE *fp; 261 262 fp = fmemopen(NULL, 16, "ab+"); 263 ATF_REQUIRE(ftell(fp) == 0L); 264 fclose(fp); 265 | 169 rc = fseek(fp, 0, SEEK_END); 170 ATF_REQUIRE(rc == 0); 171 pos = ftell(fp); 172 ATF_REQUIRE(pos == sizeof(str)); 173 174 /* Close the FILE *. */ 175 rc = fclose(fp); 176 ATF_REQUIRE(rc == 0); --- 71 unchanged lines hidden (view full) --- 248 */ 249 250 FILE *fp; 251 252 fp = fmemopen(NULL, 16, "ab+"); 253 ATF_REQUIRE(ftell(fp) == 0L); 254 fclose(fp); 255 |
266 /* 267 * Make sure that a pre-allocated buffer behaves correctly. 268 */ | 256 /* Make sure that a pre-allocated buffer behaves correctly. */ |
269 char buf[] = "Hello"; 270 fp = fmemopen(buf, sizeof(buf), "ab+"); 271 ATF_REQUIRE(ftell(fp) == strlen(buf)); 272 fclose(fp); 273} 274 275ATF_TC_WITHOUT_HEAD(test_size_0); 276ATF_TC_BODY(test_size_0, tc) 277{ | 257 char buf[] = "Hello"; 258 fp = fmemopen(buf, sizeof(buf), "ab+"); 259 ATF_REQUIRE(ftell(fp) == strlen(buf)); 260 fclose(fp); 261} 262 263ATF_TC_WITHOUT_HEAD(test_size_0); 264ATF_TC_BODY(test_size_0, tc) 265{ |
278 /* 279 * POSIX mandates that we return EINVAL if size is 0. 280 */ | 266 /* POSIX mandates that we return EINVAL if size is 0. */ |
281 282 FILE *fp; 283 284 fp = fmemopen(NULL, 0, "r+"); 285 ATF_REQUIRE(fp == NULL); 286 ATF_REQUIRE(errno == EINVAL); 287} 288 --- 12 unchanged lines hidden --- | 267 268 FILE *fp; 269 270 fp = fmemopen(NULL, 0, "r+"); 271 ATF_REQUIRE(fp == NULL); 272 ATF_REQUIRE(errno == EINVAL); 273} 274 --- 12 unchanged lines hidden --- |