test_stdio_1.c
Upload User: tsgydb
Upload Date: 2007-04-14
Package Size: 10674k
Code Size: 2k
Category:

MySQL

Development Platform:

Visual C++

  1. #include <pthread.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. char * base_name = "test_stdio_1.c";
  8. char * dir_name = SRCDIR;
  9. char * fullname;
  10. #define OK  0
  11. #define NOTOK  -1
  12. /* Test fopen()/ftell()/getc() */
  13. int test_1(void)
  14. {
  15. struct stat statbuf;
  16.     FILE * fp;
  17. int i;
  18. if (stat(fullname, &statbuf) < OK) {
  19. printf("ERROR: Couldn't stat %sn", fullname);
  20. return(NOTOK);
  21. }
  22. if ((fp = fopen(fullname, "r")) == NULL) {
  23. printf("ERROR: Couldn't open %sn", fullname);
  24. return(NOTOK);
  25. }
  26. /* Get the entire file */
  27. while ((i = getc(fp)) != EOF);
  28. if (ftell(fp) != statbuf.st_size) {
  29. printf("ERROR: ftell() and stat() don't agree.");
  30. return(NOTOK);
  31. }
  32. if (fclose(fp) < OK) {
  33. printf("ERROR: fclose() failed.");
  34. return(NOTOK);
  35. }
  36. return(OK);
  37. }
  38. /* Test fopen()/fclose() */
  39. int test_2(void)
  40. {
  41. FILE *fp1, *fp2;
  42. if ((fp1 = fopen(fullname, "r")) == NULL) {
  43. printf("ERROR: Couldn't fopen %sn", fullname);
  44. return(NOTOK);
  45. }
  46. if (fclose(fp1) < OK) {
  47. printf("ERROR: fclose() failed.");
  48. return(NOTOK);
  49. }
  50. if ((fp2 = fopen(fullname, "r")) == NULL) {
  51. printf("ERROR: Couldn't fopen %sn", fullname);
  52. return(NOTOK);
  53. }
  54. if (fclose(fp2) < OK) {
  55. printf("ERROR: fclose() failed.");
  56. return(NOTOK);
  57. }
  58. if (fp1 != fp2) {
  59. printf("ERROR: FILE table leak.n");
  60. return(NOTOK);
  61. }
  62. return(OK);
  63. }
  64. /* Test sscanf()/sprintf() */
  65. int test_3(void)
  66. {
  67.     char * str = "10 4.53";
  68. char buf[64];
  69.     double d;
  70.     int    i;
  71.     if (sscanf(str, "%d %lf", &i, &d) != 2) {
  72. printf("ERROR: sscanf didn't parse input string correctlyn");
  73. return(NOTOK);
  74. }
  75. /* Should have a check */
  76. sprintf(buf, "%d %2.2lf", i, d);
  77. if (strcmp(buf, str)) {
  78. printf("ERROR: sscanf()/sprintf() didn't parse unparse correctlyn");
  79. return(NOTOK);
  80. }
  81. return(OK);
  82. }
  83. main()
  84. {
  85. printf("test_stdio_1 STARTn");
  86. if (fullname = malloc (strlen (dir_name) + strlen (base_name) + 2)) {
  87. sprintf (fullname, "%s/%s", dir_name, base_name);
  88. } else {
  89. perror ("malloc");
  90. exit(1);
  91. }
  92. if (test_1() || test_2() || test_3()) {
  93. printf("test_stdio_1 FAILEDn");
  94. exit(1);
  95. }
  96. printf("test_stdio_1 PASSEDn");
  97. exit(0);
  98. }