test_entrypoint.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2019 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "sg_assert.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include "sg_entrypoint.h"
  31. #include <sagui.h>
  32. static void test__entrypoint_prepare(struct sg_entrypoint *entrypoint) {
  33. sg__entrypoint_prepare(entrypoint, NULL, NULL);
  34. ASSERT(!entrypoint->name);
  35. ASSERT(!entrypoint->user_data);
  36. sg__entrypoint_prepare(entrypoint, "foo", "bar");
  37. ASSERT(strcmp(entrypoint->name, "foo") == 0);
  38. ASSERT(strcmp(entrypoint->user_data, "bar") == 0);
  39. }
  40. static void test__entrypoint_cmp(struct sg_entrypoint *entrypoint1) {
  41. struct sg_entrypoint *entrypoint2 = sg_alloc(sizeof(struct sg_entrypoint));
  42. ASSERT(entrypoint2);
  43. entrypoint1->name = "foo";
  44. entrypoint2->name = "bar";
  45. ASSERT(sg__entrypoint_cmp(entrypoint1, entrypoint2) > 0);
  46. entrypoint1->name = "bar";
  47. entrypoint2->name = "foo";
  48. ASSERT(sg__entrypoint_cmp(entrypoint1, entrypoint2) < 0);
  49. entrypoint1->name = "foo";
  50. entrypoint2->name = "foo";
  51. ASSERT(sg__entrypoint_cmp(entrypoint1, entrypoint2) == 0);
  52. sg_free(entrypoint2);
  53. }
  54. static void test_entrypoint_name(struct sg_entrypoint *entrypoint) {
  55. errno = 0;
  56. ASSERT(!sg_entrypoint_name(NULL));
  57. ASSERT(errno == EINVAL);
  58. entrypoint->name = "foo";
  59. ASSERT(strcmp(sg_entrypoint_name(entrypoint), "foo") == 0);
  60. }
  61. static void test_entrypoint_set_user_data(struct sg_entrypoint *entrypoint) {
  62. const char *dummy = "foo";
  63. ASSERT(sg_entrypoint_set_user_data(NULL, (void *) dummy) == EINVAL);
  64. ASSERT(sg_entrypoint_set_user_data(entrypoint, (void *) dummy) == 0);
  65. ASSERT(strcmp(sg_entrypoint_user_data(entrypoint), "foo") == 0);
  66. dummy = "bar";
  67. ASSERT(sg_entrypoint_set_user_data(entrypoint, (void *) dummy) == 0);
  68. ASSERT(strcmp(sg_entrypoint_user_data(entrypoint), "bar") == 0);
  69. }
  70. static void test_entrypoint_user_data(struct sg_entrypoint *entrypoint) {
  71. errno = 0;
  72. ASSERT(!sg_entrypoint_user_data(NULL));
  73. ASSERT(errno == EINVAL);
  74. errno = 0;
  75. sg_entrypoint_set_user_data(entrypoint, NULL);
  76. ASSERT(!sg_entrypoint_user_data(entrypoint));
  77. ASSERT(errno == 0);
  78. sg_entrypoint_set_user_data(entrypoint, "foo");
  79. ASSERT(strcmp(sg_entrypoint_user_data(entrypoint), "foo") == 0);
  80. sg_entrypoint_set_user_data(entrypoint, "bar");
  81. ASSERT(strcmp(sg_entrypoint_user_data(entrypoint), "bar") == 0);
  82. }
  83. int main(void) {
  84. struct sg_entrypoint *entrypoint = sg_alloc(sizeof(struct sg_entrypoint));
  85. ASSERT(entrypoint);
  86. test__entrypoint_prepare(entrypoint);
  87. test__entrypoint_cmp(entrypoint);
  88. test_entrypoint_name(entrypoint);
  89. test_entrypoint_set_user_data(entrypoint);
  90. test_entrypoint_user_data(entrypoint);
  91. sg_free(entrypoint);
  92. return EXIT_SUCCESS;
  93. }