PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libgsasl/libgsasl-1.0/gltests/test-getdelim.c

https://bitbucket.org/thelearninglabs/uclinux-distro-tll-public
C | 90 lines | 58 code | 11 blank | 21 comment | 15 complexity | 05d73de83ab56893943fc10f74e934d1 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, Unlicense, GPL-2.0, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0, ISC, MIT, 0BSD, LGPL-2.0
  1. /* Test of getdelim() function.
  2. Copyright (C) 2007-2008 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /* Written by Eric Blake <ebb9@byu.net>, 2007. */
  15. #include <config.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #define ASSERT(expr) \
  20. do \
  21. { \
  22. if (!(expr)) \
  23. { \
  24. fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
  25. fflush (stderr); \
  26. abort (); \
  27. } \
  28. } \
  29. while (0)
  30. int
  31. main (int argc, char **argv)
  32. {
  33. FILE *f;
  34. char *line = NULL;
  35. size_t len = 0;
  36. ssize_t result;
  37. /* Create test file. */
  38. f = fopen ("test-getdelim.txt", "wb");
  39. if (!f || fwrite ("anbcnd\0f", 1, 8, f) != 8 || fclose (f) != 0)
  40. {
  41. fputs ("Failed to create sample file.\n", stderr);
  42. remove ("test-getdelim.txt");
  43. return 1;
  44. }
  45. f = fopen ("test-getdelim.txt", "rb");
  46. if (!f)
  47. {
  48. fputs ("Failed to reopen sample file.\n", stderr);
  49. remove ("test-getdelim.txt");
  50. return 1;
  51. }
  52. /* Test initial allocation, which must include trailing NUL. */
  53. result = getdelim (&line, &len, 'n', f);
  54. ASSERT (result == 2);
  55. ASSERT (strcmp (line, "an") == 0);
  56. ASSERT (2 < len);
  57. /* Test growth of buffer. */
  58. free (line);
  59. line = malloc (1);
  60. len = 1;
  61. result = getdelim (&line, &len, 'n', f);
  62. ASSERT (result == 3);
  63. ASSERT (strcmp (line, "bcn") == 0);
  64. ASSERT (3 < len);
  65. /* Test embedded NULs and EOF behavior. */
  66. result = getdelim (&line, &len, 'n', f);
  67. ASSERT (result == 3);
  68. ASSERT (memcmp (line, "d\0f", 4) == 0);
  69. ASSERT (3 < len);
  70. result = getdelim (&line, &len, 'n', f);
  71. ASSERT (result == -1);
  72. free (line);
  73. fclose (f);
  74. remove ("test-getdelim.txt");
  75. return 0;
  76. }