test_elt.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @file test_elt.h
  3. */
  4. #ifndef TEST_ELT_H
  5. #define TEST_ELT_H
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include "merge_print_ctxt.h"
  9. #include "doc_stream.h"
  10. #include "merge_delta.h"
  11. #include "doc_elt.h"
  12. #include "doc_elt_ops.h"
  13. static void test_elt_print_op (doc_elt *elt, void *ctxt, doc_stream *out);
  14. static void test_elt_merge_print_op (merge_delta *delta, merge_print_ctxt *ctxt,
  15. doc_stream *out);
  16. static bool test_elt_compare_op (doc_elt *a, doc_src a_src,
  17. doc_elt *b, doc_src b_src, void *ctxt);
  18. /**
  19. * @struct test_elt
  20. * @brief An extremely simple doc_elt used for testing.
  21. */
  22. typedef struct test_elt
  23. {
  24. doc_elt elt;
  25. int value;
  26. int merge_print_depth;
  27. } test_elt;
  28. static doc_elt_ops test_elt_ops = {
  29. .print = test_elt_print_op,
  30. .merge_print = test_elt_merge_print_op,
  31. .compare = test_elt_compare_op
  32. };
  33. static int
  34. test_elt_get_value (test_elt *elt)
  35. {
  36. return elt->value;
  37. }
  38. static void
  39. test_elt_set_value (test_elt *elt, int value)
  40. {
  41. elt->value = value;
  42. return;
  43. }
  44. static int
  45. test_elt_get_merge_print_depth (test_elt *elt)
  46. {
  47. return elt->merge_print_depth;
  48. }
  49. static void
  50. test_elt_set_merge_print_depth (test_elt *elt, int depth)
  51. {
  52. elt->merge_print_depth = depth;
  53. return;
  54. }
  55. /**
  56. * Reset values in elt changed by doc_elt_ops to zero.
  57. */
  58. static void
  59. test_elt_reset (test_elt *elt)
  60. {
  61. test_elt_set_merge_print_depth (elt, 0);
  62. }
  63. static void
  64. test_elt_print_op (doc_elt *elt, void *ctxt, doc_stream *out)
  65. {
  66. /* do nothing */
  67. }
  68. static void
  69. test_elt_merge_print_op (merge_delta *delta, merge_print_ctxt *ctxt, doc_stream *out)
  70. {
  71. doc_elt *elt = merge_delta_get_elt (delta);
  72. test_elt_set_merge_print_depth ((test_elt *)elt, ctxt->depth);
  73. ctxt->depth++;
  74. return;
  75. }
  76. static bool
  77. test_elt_compare_op (doc_elt *a, doc_src a_src,
  78. doc_elt *b, doc_src b_src, void *ctxt)
  79. {
  80. doc_elt_ops *a_ops = doc_elt_get_ops (a);
  81. doc_elt_ops *b_ops = doc_elt_get_ops (b);
  82. assert (a_ops == b_ops);
  83. assert (a_ops == &test_elt_ops);
  84. assert (b_ops == &test_elt_ops);
  85. return (test_elt_get_value ((test_elt*)a) == test_elt_get_value ((test_elt *)b));
  86. }
  87. static test_elt *
  88. test_elt_create (int value)
  89. {
  90. test_elt *elt = malloc (sizeof (test_elt));
  91. doc_elt_set_ops (&(elt->elt), &test_elt_ops);
  92. test_elt_set_value (elt, value);
  93. test_elt_set_merge_print_depth (elt, 0);
  94. }
  95. static void
  96. test_elt_free (test_elt *elt)
  97. {
  98. free (elt);
  99. }
  100. #endif