1
0

test-util.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <string.h>
  24. #define memeq(a, b, c) (memcmp((a), (b), (c)) == 0)
  25. TEST_IMPL(strlcpy) {
  26. size_t r;
  27. {
  28. char dst[2] = "A";
  29. r = uv_strlcpy(dst, "", 0);
  30. ASSERT(r == 0);
  31. ASSERT(memeq(dst, "A", 1));
  32. }
  33. {
  34. char dst[2] = "A";
  35. r = uv_strlcpy(dst, "B", 1);
  36. ASSERT(r == 0);
  37. ASSERT(memeq(dst, "", 1));
  38. }
  39. {
  40. char dst[2] = "A";
  41. r = uv_strlcpy(dst, "B", 2);
  42. ASSERT(r == 1);
  43. ASSERT(memeq(dst, "B", 2));
  44. }
  45. {
  46. char dst[3] = "AB";
  47. r = uv_strlcpy(dst, "CD", 3);
  48. ASSERT(r == 2);
  49. ASSERT(memeq(dst, "CD", 3));
  50. }
  51. return 0;
  52. }
  53. TEST_IMPL(strlcat) {
  54. size_t r;
  55. {
  56. char dst[2] = "A";
  57. r = uv_strlcat(dst, "B", 1);
  58. ASSERT(r == 1);
  59. ASSERT(memeq(dst, "A", 2));
  60. }
  61. {
  62. char dst[2] = "A";
  63. r = uv_strlcat(dst, "B", 2);
  64. ASSERT(r == 1);
  65. ASSERT(memeq(dst, "A", 2));
  66. }
  67. {
  68. char dst[3] = "A";
  69. r = uv_strlcat(dst, "B", 3);
  70. ASSERT(r == 2);
  71. ASSERT(memeq(dst, "AB", 3));
  72. }
  73. {
  74. char dst[5] = "AB";
  75. r = uv_strlcat(dst, "CD", 5);
  76. ASSERT(r == 4);
  77. ASSERT(memeq(dst, "ABCD", 5));
  78. }
  79. return 0;
  80. }