test-utils.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <assert.h>
  36. #include <string.h>
  37. #include <math.h>
  38. #include <errno.h>
  39. #include "utils.h"
  40. int
  41. main(void)
  42. {
  43. long long int ll;
  44. long long int lli;
  45. double dbl;
  46. double dbli;
  47. char buf[32];
  48. assert(utils_strtonum("0", 0, 100, &ll) == 0);
  49. assert(ll == 0);
  50. assert(utils_strtonum("100", 0, 100, &ll) == 0);
  51. assert(ll == 100);
  52. assert(utils_strtonum("101", 0, 100, &ll) != 0);
  53. assert(utils_strtonum("0", 1, 100, &ll) != 0);
  54. errno = ERANGE;
  55. assert(utils_strtonum("10", 0, 100, &ll) == 0);
  56. assert(ll == 10);
  57. assert(utils_strtonum("-1", -1, 0, &ll) == 0);
  58. assert(ll == -1);
  59. assert(utils_strtonum("-10", -20, -10, &ll) == 0);
  60. assert(ll == -10);
  61. assert(utils_strtonum("0", 1, 0, &ll) == -1);
  62. for (lli = -100; lli <= 100; lli++) {
  63. assert(snprintf(buf, sizeof(buf), "%lld", lli) > 0);
  64. assert(utils_strtonum(buf, -100, 100, &ll) == 0);
  65. assert(ll == lli);
  66. }
  67. assert(utils_strtonum("test", -1000, 1000, &ll) == -1);
  68. assert(utils_strtonum("12a", -1000, 1000, &ll) == -1);
  69. assert(utils_parse_bool_str("on") == 1);
  70. assert(utils_parse_bool_str("yes") == 1);
  71. assert(utils_parse_bool_str("1") == 1);
  72. assert(utils_parse_bool_str("ON") == 1);
  73. assert(utils_parse_bool_str("YeS") == 1);
  74. assert(utils_parse_bool_str("off") == 0);
  75. assert(utils_parse_bool_str("no") == 0);
  76. assert(utils_parse_bool_str("0") == 0);
  77. assert(utils_parse_bool_str("oFf") == 0);
  78. assert(utils_parse_bool_str("of") == -1);
  79. assert(utils_parse_bool_str("noo") == -1);
  80. assert(utils_parse_bool_str("01") == -1);
  81. assert(utils_strtod("0", 0, 100, &dbl) == 0);
  82. assert(dbl == 0);
  83. assert(utils_strtod("0.0", 0, 100, &dbl) == 0);
  84. assert(dbl == 0);
  85. assert(utils_strtod("100", 0, 100, &dbl) == 0);
  86. assert(dbl == 100);
  87. assert(utils_strtod("100.0", 0, 100, &dbl) == 0);
  88. assert(dbl == 100);
  89. assert(utils_strtod("99.9", 0, 100, &dbl) == 0);
  90. assert(dbl == 99.9);
  91. assert(utils_strtod("101", 0, 100, &dbl) != 0);
  92. assert(utils_strtod("0", 1, 100, &dbl) != 0);
  93. errno = ERANGE;
  94. assert(utils_strtod("10", 0, 100, &dbl) == 0);
  95. assert(dbl == 10);
  96. assert(utils_strtod("-1", -1, 0, &dbl) == 0);
  97. assert(dbl == -1);
  98. assert(utils_strtod("-10", -20, -10, &dbl) == 0);
  99. assert(dbl == -10);
  100. assert(utils_strtod("0", 1, 0, &dbl) == -1);
  101. for (dbli = -100; dbli <= 100; dbli += 0.01) {
  102. assert(snprintf(buf, sizeof(buf), "%f", dbli) > 0);
  103. assert(utils_strtod(buf, -100, 101, &dbl) == 0);
  104. assert(fabs(dbl - dbli) < 0.0001);
  105. }
  106. assert(utils_strtod("test", -1000, 1000, &dbl) == -1);
  107. assert(utils_strtod("12a", -1000, 1000, &dbl) == -1);
  108. assert(utils_strtod("inf", -1000, 1000, &dbl) == -1);
  109. assert(utils_strtod("nan", -1000, 1000, &dbl) == -1);
  110. return (0);
  111. }