tst-strfrom-locale.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Tests for strfromf, strfromd, strfroml functions.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "tst-strfrom.h"
  16. static const struct test tests[] = {
  17. TEST ("12,345000", "%f", 50, 9, 12.345),
  18. TEST ("9,999", "%.3f", 50, 5, 9.999),
  19. TEST ("0,125000", "%f", 50, 8, .125),
  20. TEST ("0,000000", "%f", 50, 8, .0),
  21. TEST ("0", "%g", 50, 1, .0),
  22. TEST ("9,900000", "%f", 50, 8, 9.9),
  23. TEST ("9,1", "%.5f", 4, 7, 9.123456),
  24. TEST ("9,91235", "%g", 50, 7, 9.91234567812345678),
  25. TEST ("79,8765", "%G", 50, 7, 79.8765432111),
  26. TEST ("79,9", "%.3g", 50, 4, 79.8765432111),
  27. TEST ("1,000000e+38", "%e", 50, 12, 1e+38),
  28. TEST ("1,000000e+38", "%e", 50, 12, 1e38),
  29. TEST ("-1,000000e-37", "%e", 50, 13, -1e-37),
  30. TEST ("1,000000e-37", "%e", 50, 12, 0.00000001e-29),
  31. TEST ("1,000000e-37", "%e", 50, 12, 1.000000e-37),
  32. TEST ("5,900000e-16", "%e", 50, 12, 5.9e-16),
  33. TEST ("1,234500e+20", "%e", 50, 12, 12.345e19),
  34. TEST ("1,000000e+05", "%e", 50, 12, 1e5),
  35. TEST ("-NAN", "%G", 50, 4, -NAN_),
  36. TEST ("-inf", "%g", 50, 4, -INF),
  37. TEST ("inf", "%g", 50, 3, INF)
  38. };
  39. /* Tests with buffer size small. */
  40. static const struct test stest[] = {
  41. TEST ("1234", "%g", 5, 7, 12345.345),
  42. TEST ("0,12", "%f", 5, 8, .125),
  43. TEST ("9,99", "%.3f", 5, 5, 9.999),
  44. TEST ("100", "%g", 5, 3, 1e2)
  45. };
  46. /* Hexadecimal tests. */
  47. static const struct htests htest[] = {
  48. HTEST ("%a", { "0x1,ffp+6", "0x3,fep+5", "0x7,fcp+4", "0xf,f8p+3" },
  49. 0x1.ffp+6),
  50. HTEST ("%a", { "0x1,88p+4", "0x3,1p+3", "0x6,2p+2", "0xc,4p+1" },
  51. 0x1.88p+4),
  52. HTEST ("%A", { "-0X1,88P+5", "-0X3,1P+4", "-0X6,2P+3", "-0XC,4P+2" },
  53. -0x1.88p+5),
  54. HTEST ("%a", { "0x1,44p+10", "0x2,88p+9", "0x5,1p+8", "0xa,2p+7" },
  55. 0x1.44p+10),
  56. HTEST ("%a", { "0x1p-10", "0x2p-11", "0x4p-12", "0x8p-13" },
  57. 0x0.0040p+0),
  58. HTEST ("%a", { "0x1,4p+3", "0x2,8p+2", "0x5p+1", "0xap+0" },
  59. 10.0)
  60. };
  61. GEN_TEST_STRTOD_FOREACH (TEST_STRFROM)
  62. static int
  63. test_locale (const char *locale)
  64. {
  65. printf ("Testing in locale: %s\n", locale);
  66. if (setlocale (LC_ALL, locale) == NULL)
  67. {
  68. printf ("Cannot set locale %s\n", locale);
  69. }
  70. return STRTOD_TEST_FOREACH (test_);
  71. }
  72. static int
  73. do_test (void)
  74. {
  75. int result = 0;
  76. result += test_locale ("de_DE.UTF-8");
  77. result += test_locale ("tr_TR.ISO-8859-9");
  78. result += test_locale ("tr_TR.UTF-8");
  79. return result;
  80. }
  81. #define TEST_FUNCTION do_test ()
  82. #include "../test-skeleton.c"