test_fmt.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  3. *
  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. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include <assert.h>
  19. #include <string.h>
  20. #include <kern/error.h>
  21. #include <kern/fmt.h>
  22. #include <kern/init.h>
  23. #include <kern/log.h>
  24. #include <test/test.h>
  25. static void
  26. test_printf_putc(void *data, int ch)
  27. {
  28. char **ptr;
  29. ptr = data;
  30. **ptr = ch < 'A' || ch > 'Z' ? ch : ((ch - 'A') + 'a');
  31. (*ptr)++;
  32. }
  33. static void
  34. test_sscanf(void)
  35. {
  36. int rv, x1, x2;
  37. char c1, c2;
  38. rv = fmt_sscanf("123 qx -45", "%d %c%c %d", &x1, &c1, &c2, &x2);
  39. assert(rv == 4);
  40. assert(x1 == 123);
  41. assert(c1 == 'q');
  42. assert(c2 == 'x');
  43. assert(x2 == -45);
  44. }
  45. void __init
  46. test_setup(void)
  47. {
  48. char buf[32], *p;
  49. struct fmt_write_op write_op;
  50. int rv;
  51. rv = fmt_sprintf(buf, "hello %d %s", -4, "???");
  52. assert(rv == 12);
  53. rv = strcmp(buf, "hello -4 ???");
  54. assert(rv == 0);
  55. rv = fmt_snprintf(buf, 4, "abc%d", 33);
  56. assert(rv == 5);
  57. buf[rv - 1] = '\0';
  58. rv = strcmp(buf, "abc3");
  59. assert(rv == 0);
  60. p = buf;
  61. write_op.data = &p;
  62. write_op.putc = test_printf_putc;
  63. rv = fmt_xprintf(&write_op, "HELLO %d", -1);
  64. assert(rv > 0);
  65. buf[rv] = '\0';
  66. assert(strcmp(buf, "hello -1") == 0);
  67. test_sscanf();
  68. log_info("fmt test done");
  69. }