test_stdlib.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* This file is part of asmc, a bootstrapping OS with minimal seed
  2. Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. https://gitlab.com/giomasce/asmc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <stdlib.h>
  15. int test_malloc_free() {
  16. void *ptr;
  17. ptr = malloc(10);
  18. free(ptr);
  19. return ptr != NULL;
  20. }
  21. int test_calloc_free() {
  22. void *ptr;
  23. ptr = calloc(50, 20);
  24. free(ptr);
  25. return ptr != NULL;
  26. }
  27. int test_malloc_realloc_free() {
  28. char *ptr;
  29. ptr = malloc(100);
  30. *ptr = 'A';
  31. *(ptr+1) = 'B';
  32. *(ptr+49) = 'Z';
  33. ptr = realloc(ptr, 50);
  34. if (*ptr != 'A') return 0;
  35. if (*(ptr+1) != 'B') return 0;
  36. if (*(ptr+49) != 'Z') return 0;
  37. ptr = realloc(ptr, 200);
  38. if (*ptr != 'A') return 0;
  39. if (*(ptr+1) != 'B') return 0;
  40. if (*(ptr+49) != 'Z') return 0;
  41. free(ptr);
  42. return ptr != NULL;
  43. }
  44. int test_free_null() {
  45. free(NULL);
  46. free(0);
  47. return 1;
  48. }
  49. int test_realloc_null_free() {
  50. char *ptr = realloc(NULL, 100);
  51. free(ptr);
  52. return ptr != NULL;
  53. }
  54. static int compare( const void * left, const void * right )
  55. {
  56. return *( (unsigned char *)left ) - *( (unsigned char *)right );
  57. }
  58. char presort[] = "shreicnyjqpvozxmbt";
  59. char sorted1[] = "bcehijmnopqrstvxyz";
  60. char sorted2[] = "bticjqnyozpvreshxm";
  61. // From PDClib
  62. int test_qsort() {
  63. char s[19];
  64. strcpy( s, presort );
  65. qsort( s, 18, 1, compare );
  66. if ( strcmp( s, sorted1 ) != 0 ) return 0;
  67. strcpy( s, presort );
  68. qsort( s, 9, 2, compare );
  69. if ( strcmp( s, sorted2 ) != 0 ) return 0;
  70. strcpy( s, presort );
  71. qsort( s, 1, 1, compare );
  72. if ( strcmp( s, presort ) != 0 ) return 0;
  73. qsort( s, 100, 0, compare );
  74. if ( strcmp( s, presort ) != 0 ) return 0;
  75. return 1;
  76. }
  77. int test_strtoull_zero() {
  78. char *p = "0";
  79. char *q;
  80. unsigned long long n = strtoull(p, &q, 0);
  81. if (*q != '\0') return 0;
  82. if (n != 0) return 0;
  83. return 1;
  84. }