test-cplusplus.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright © 2011 Google, Inc.
  3. * Copyright © 2022 Behdad Esfahbod
  4. *
  5. * This is part of HarfBuzz, a text shaping library.
  6. *
  7. * Permission is hereby granted, without written agreement and without
  8. * license or royalty fees, to use, copy, modify, and distribute this
  9. * software and its documentation for any purpose, provided that the
  10. * above copyright notice and the following two paragraphs appear in
  11. * all copies of this software.
  12. *
  13. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  14. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  15. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  16. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  17. * DAMAGE.
  18. *
  19. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  20. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  21. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  22. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  23. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  24. *
  25. * Google Author(s): Behdad Esfahbod
  26. */
  27. /* This file tests that all headers can be included from C++ files,
  28. * as well as test the C++ API. */
  29. #ifdef HAVE_CONFIG_H
  30. #include <config.h>
  31. #endif
  32. #include <hb.h>
  33. #include <hb-subset.h>
  34. #include <hb-ot.h>
  35. #include <hb-aat.h>
  36. #ifdef HAVE_GLIB
  37. #include <hb-glib.h>
  38. #endif
  39. #ifdef HAVE_ICU
  40. #include <hb-icu.h>
  41. #endif
  42. #ifdef HAVE_FREETYPE
  43. #include <hb-ft.h>
  44. #endif
  45. #ifdef HAVE_UNISCRIBE
  46. #include <hb-uniscribe.h>
  47. #endif
  48. #ifdef HAVE_CORETEXT
  49. #include <hb-coretext.h>
  50. #endif
  51. /* Test C++ API. */
  52. #include "hb-cplusplus.hh"
  53. #include <cassert>
  54. #include <functional>
  55. #include <utility>
  56. int
  57. main ()
  58. {
  59. hb_buffer_t *b = hb_buffer_create ();
  60. hb::shared_ptr<hb_buffer_t> pb {b};
  61. /* Test copy-construction. */
  62. assert (bool (pb));
  63. hb::shared_ptr<hb_buffer_t> pb2 {pb};
  64. assert (bool (pb2));
  65. assert (bool (pb));
  66. /* Test move-construction. */
  67. assert (bool (pb2));
  68. hb::shared_ptr<hb_buffer_t> pb4 {std::move (pb2)};
  69. assert (!bool (pb2));
  70. assert (bool (pb4));
  71. /* Test copy-assignment. */
  72. hb::shared_ptr<hb_buffer_t> pb3;
  73. assert (!bool (pb3));
  74. pb3 = pb;
  75. assert (bool (pb3));
  76. assert (bool (pb));
  77. /* Test move-assignment. */
  78. assert (bool (pb));
  79. pb2 = std::move (pb);
  80. assert (!bool (pb));
  81. pb.reference ();
  82. pb.destroy ();
  83. pb3.reference ();
  84. pb3.destroy ();
  85. pb3.swap (pb4);
  86. hb_user_data_key_t key;
  87. pb.set_user_data (&key, b, nullptr, true);
  88. (void) pb.get_user_data (&key);
  89. hb::unique_ptr<hb_buffer_t> pb5 {pb3.reference ()};
  90. /* Test that shared_ptr / unique_ptr are std::hash'able, and that they
  91. * return the same hash (which is the underlying pointer's hash. */
  92. std::hash<hb_buffer_t *> hash {};
  93. std::hash<hb::shared_ptr<hb_buffer_t>> hash2 {};
  94. std::hash<hb::unique_ptr<hb_buffer_t>> hash3 {};
  95. assert (hash (b) == hash2 (pb4));
  96. assert (hash2 (pb4) == hash2 (pb2));
  97. assert (hash (b) == hash3 (pb5));
  98. return pb == pb.get_empty () || pb == pb2;
  99. }