summaryrefslogtreecommitdiff
path: root/tests/test_cpp.cc
blob: abbafdabc8fa215956f61185f856237d0b640990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/*
 *
 * Tests for C++ wrappers.
 */

#include <stdio.h>
#include <string.h>

#include <fstream>
#include <iostream>
#include <set>
#include <sstream>

#include "tests/test_cpp.upbdefs.h"
#include "tests/upb_test.h"
#include "upb/def.h"
#include "upb/handlers.h"
#include "upb/pb/decoder.h"
#include "upb/pb/textprinter.h"
#include "upb/port_def.inc"
#include "upb/upb.h"

template <class T>
void AssertInsert(T* const container, const typename T::value_type& val) {
  bool inserted = container->insert(val).second;
  ASSERT(inserted);
}

//
// Tests for registering and calling handlers in all their variants.
// This test code is very repetitive because we have to declare each
// handler function variant separately, and they all have different
// signatures so it does not lend itself well to templates.
//
// We test three handler types:
//   StartMessage (no data params)
//   Int32        (1 data param (int32_t))
//   String Buf   (2 data params (const char*, size_t))
//
// For each handler type we test all 8 handler variants:
//   (handler data?) x  (function/method) x (returns {void, success})
//
// The one notable thing we don't test at the moment is
// StartSequence/StartString handlers: these are different from StartMessage()
// in that they return void* for the sub-closure.  But this is exercised in
// other tests.
//

static const int kExpectedHandlerData = 1232323;

class StringBufTesterBase {
 public:
  static const int kFieldNumber = 3;

  StringBufTesterBase() : seen_(false), handler_data_val_(0) {}

  void CallAndVerify(upb::Sink sink, upb::FieldDefPtr f) {
    upb_selector_t start;
    ASSERT(upb_handlers_getselector(f.ptr(), UPB_HANDLER_STARTSTR, &start));
    upb_selector_t str;
    ASSERT(upb_handlers_getselector(f.ptr(), UPB_HANDLER_STRING, &str));

    ASSERT(!seen_);
    upb::Sink sub;
    sink.StartMessage();
    sink.StartString(start, 0, &sub);
    size_t ret = sub.PutStringBuffer(str, &buf_, 5, &handle_);
    ASSERT(seen_);
    ASSERT(len_ == 5);
    ASSERT(ret == 5);
    ASSERT(handler_data_val_ == kExpectedHandlerData);
  }

 protected:
  bool seen_;
  int handler_data_val_;
  size_t len_;
  char buf_;
  upb_bufhandle handle_;
};

// Test 8 combinations of:
//   (handler data?) x (buffer handle?) x (function/method)
//
// Then we add one test each for this variation: to prevent combinatorial
// explosion of these tests we don't test the full 16 combinations, but
// rely on our knowledge that the implementation processes the return wrapping
// in a second separate and independent stage:
//
//   (function/method)

class StringBufTesterVoidMethodNoHandlerDataNoHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterVoidMethodNoHandlerDataNoHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  void Handler(const char *buf, size_t len) {
    ASSERT(buf == &buf_);
    seen_ = true;
    len_ = len;
  }
};

class StringBufTesterVoidMethodNoHandlerDataWithHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterVoidMethodNoHandlerDataWithHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  void Handler(const char *buf, size_t len, const upb_bufhandle* handle) {
    ASSERT(buf == &buf_);
    ASSERT(handle == &handle_);
    seen_ = true;
    len_ = len;
  }
};

class StringBufTesterVoidMethodWithHandlerDataNoHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterVoidMethodWithHandlerDataNoHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(
        f, UpbBind(&ME::Handler, new int(kExpectedHandlerData))));
  }

 private:
  void Handler(const int* hd, const char *buf, size_t len) {
    ASSERT(buf == &buf_);
    handler_data_val_ = *hd;
    seen_ = true;
    len_ = len;
  }
};

class StringBufTesterVoidMethodWithHandlerDataWithHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterVoidMethodWithHandlerDataWithHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(
        f, UpbBind(&ME::Handler, new int(kExpectedHandlerData))));
  }

 private:
  void Handler(const int* hd, const char* buf, size_t len,
               const upb_bufhandle* handle) {
    ASSERT(buf == &buf_);
    ASSERT(handle == &handle_);
    handler_data_val_ = *hd;
    seen_ = true;
    len_ = len;
  }
};

class StringBufTesterVoidFunctionNoHandlerDataNoHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterVoidFunctionNoHandlerDataNoHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  static void Handler(ME* t, const char *buf, size_t len) {
    ASSERT(buf == &t->buf_);
    t->seen_ = true;
    t->len_ = len;
  }
};

class StringBufTesterVoidFunctionNoHandlerDataWithHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterVoidFunctionNoHandlerDataWithHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  static void Handler(ME* t, const char* buf, size_t len,
                      const upb_bufhandle* handle) {
    ASSERT(buf == &t->buf_);
    ASSERT(handle == &t->handle_);
    t->seen_ = true;
    t->len_ = len;
  }
};

class StringBufTesterVoidFunctionWithHandlerDataNoHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterVoidFunctionWithHandlerDataNoHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(
        f, UpbBind(&ME::Handler, new int(kExpectedHandlerData))));
  }

 private:
  static void Handler(ME* t, const int* hd, const char *buf, size_t len) {
    ASSERT(buf == &t->buf_);
    t->handler_data_val_ = *hd;
    t->seen_ = true;
    t->len_ = len;
  }
};

class StringBufTesterVoidFunctionWithHandlerDataWithHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterVoidFunctionWithHandlerDataWithHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(
        f, UpbBind(&ME::Handler, new int(kExpectedHandlerData))));
  }

 private:
  static void Handler(ME* t, const int* hd, const char* buf, size_t len,
                      const upb_bufhandle* handle) {
    ASSERT(buf == &t->buf_);
    ASSERT(handle == &t->handle_);
    t->handler_data_val_ = *hd;
    t->seen_ = true;
    t->len_ = len;
  }
};

class StringBufTesterSizeTMethodNoHandlerDataNoHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterSizeTMethodNoHandlerDataNoHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  size_t Handler(const char *buf, size_t len) {
    ASSERT(buf == &buf_);
    seen_ = true;
    len_ = len;
    return len;
  }
};

class StringBufTesterBoolMethodNoHandlerDataNoHandle
    : public StringBufTesterBase {
 public:
  typedef StringBufTesterBoolMethodNoHandlerDataNoHandle ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  bool Handler(const char *buf, size_t len) {
    ASSERT(buf == &buf_);
    seen_ = true;
    len_ = len;
    return true;
  }
};

class StartMsgTesterBase {
 public:
  // We don't need the FieldDef it will create, but the test harness still
  // requires that we provide one.
  static const int kFieldNumber = 3;

  StartMsgTesterBase() : seen_(false), handler_data_val_(0) {}

  void CallAndVerify(upb::Sink sink, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(!seen_);
    sink.StartMessage();
    ASSERT(seen_);
    ASSERT(handler_data_val_ == kExpectedHandlerData);
  }

 protected:
  bool seen_;
  int handler_data_val_;
};

// Test all 8 combinations of:
//   (handler data?) x  (function/method) x (returns {void, bool})

class StartMsgTesterVoidFunctionNoHandlerData : public StartMsgTesterBase {
 public:
  typedef StartMsgTesterVoidFunctionNoHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStartMessageHandler(UpbMakeHandler(&Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  //static void Handler(ME* t) {
  static void Handler(ME* t) {
    t->seen_ = true;
  }
};

class StartMsgTesterBoolFunctionNoHandlerData : public StartMsgTesterBase {
 public:
  typedef StartMsgTesterBoolFunctionNoHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStartMessageHandler(UpbMakeHandler(&Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  static bool Handler(ME* t) {
    t->seen_ = true;
    return true;
  }
};

class StartMsgTesterVoidMethodNoHandlerData : public StartMsgTesterBase {
 public:
  typedef StartMsgTesterVoidMethodNoHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStartMessageHandler(UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  void Handler() {
    seen_ = true;
  }
};

class StartMsgTesterBoolMethodNoHandlerData : public StartMsgTesterBase {
 public:
  typedef StartMsgTesterBoolMethodNoHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStartMessageHandler(UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  bool Handler() {
    seen_ = true;
    return true;
  }
};

class StartMsgTesterVoidFunctionWithHandlerData : public StartMsgTesterBase {
 public:
  typedef StartMsgTesterVoidFunctionWithHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStartMessageHandler(
        UpbBind(&Handler, new int(kExpectedHandlerData))));
  }

 private:
  static void Handler(ME* t, const int* hd) {
    t->handler_data_val_ = *hd;
    t->seen_ = true;
  }
};

class StartMsgTesterBoolFunctionWithHandlerData : public StartMsgTesterBase {
 public:
  typedef StartMsgTesterBoolFunctionWithHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStartMessageHandler(
        UpbBind(&Handler, new int(kExpectedHandlerData))));
  }

 private:
  static bool Handler(ME* t, const int* hd) {
    t->handler_data_val_ = *hd;
    t->seen_ = true;
    return true;
  }
};

class StartMsgTesterVoidMethodWithHandlerData : public StartMsgTesterBase {
 public:
  typedef StartMsgTesterVoidMethodWithHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStartMessageHandler(
        UpbBind(&ME::Handler, new int(kExpectedHandlerData))));
  }

 private:
  void Handler(const int* hd) {
    handler_data_val_ = *hd;
    seen_ = true;
  }
};

class StartMsgTesterBoolMethodWithHandlerData : public StartMsgTesterBase {
 public:
  typedef StartMsgTesterBoolMethodWithHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    UPB_UNUSED(f);
    ASSERT(h.SetStartMessageHandler(
        UpbBind(&ME::Handler, new int(kExpectedHandlerData))));
  }

 private:
  bool Handler(const int* hd) {
    handler_data_val_ = *hd;
    seen_ = true;
    return true;
  }
};

class Int32ValueTesterBase {
 public:
  static const int kFieldNumber = 1;

  Int32ValueTesterBase() : seen_(false), val_(0), handler_data_val_(0) {}

  void CallAndVerify(upb::Sink sink, upb::FieldDefPtr f) {
    upb_selector_t s;
    ASSERT(upb_handlers_getselector(f.ptr(), UPB_HANDLER_INT32, &s));

    ASSERT(!seen_);
    sink.PutInt32(s, 5);
    ASSERT(seen_);
    ASSERT(handler_data_val_ == kExpectedHandlerData);
    ASSERT(val_ == 5);
  }

 protected:
  bool seen_;
  int32_t val_;
  int handler_data_val_;
};

// Test all 8 combinations of:
//   (handler data?) x  (function/method) x (returns {void, bool})

class ValueTesterInt32VoidFunctionNoHandlerData
    : public Int32ValueTesterBase {
 public:
  typedef ValueTesterInt32VoidFunctionNoHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    ASSERT(h.SetInt32Handler(f, UpbMakeHandler(&Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  static void Handler(ME* t, int32_t val) {
    t->val_ = val;
    t->seen_ = true;
  }
};

class ValueTesterInt32BoolFunctionNoHandlerData
    : public Int32ValueTesterBase {
 public:
  typedef ValueTesterInt32BoolFunctionNoHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    ASSERT(h.SetInt32Handler(f, UpbMakeHandler(&Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  static bool Handler(ME* t, int32_t val) {
    t->val_ = val;
    t->seen_ = true;
    return true;
  }
};

class ValueTesterInt32VoidMethodNoHandlerData : public Int32ValueTesterBase {
 public:
  typedef ValueTesterInt32VoidMethodNoHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    ASSERT(h.SetInt32Handler(f, UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  void Handler(int32_t val) {
    val_ = val;
    seen_ = true;
  }
};

class ValueTesterInt32BoolMethodNoHandlerData : public Int32ValueTesterBase {
 public:
  typedef ValueTesterInt32BoolMethodNoHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    ASSERT(h.SetInt32Handler(f, UpbMakeHandler(&ME::Handler)));
    handler_data_val_ = kExpectedHandlerData;
  }

 private:
  bool Handler(int32_t val) {
    val_ = val;
    seen_ = true;
    return true;
  }
};

class ValueTesterInt32VoidFunctionWithHandlerData
    : public Int32ValueTesterBase {
 public:
  typedef ValueTesterInt32VoidFunctionWithHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    ASSERT(h.SetInt32Handler(
        f, UpbBind(&Handler, new int(kExpectedHandlerData))));
  }

 private:
  static void Handler(ME* t, const int* hd, int32_t val) {
    t->val_ = val;
    t->handler_data_val_ = *hd;
    t->seen_ = true;
  }
};

class ValueTesterInt32BoolFunctionWithHandlerData
    : public Int32ValueTesterBase {
 public:
  typedef ValueTesterInt32BoolFunctionWithHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    ASSERT(h.SetInt32Handler(
        f, UpbBind(&Handler, new int(kExpectedHandlerData))));
  }

 private:
  static bool Handler(ME* t, const int* hd, int32_t val) {
    t->val_ = val;
    t->handler_data_val_ = *hd;
    t->seen_ = true;
    return true;
  }
};

class ValueTesterInt32VoidMethodWithHandlerData : public Int32ValueTesterBase {
 public:
  typedef ValueTesterInt32VoidMethodWithHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    ASSERT(h.SetInt32Handler(
        f, UpbBind(&ME::Handler, new int(kExpectedHandlerData))));
  }

 private:
  void Handler(const int* hd, int32_t val) {
    val_ = val;
    handler_data_val_ = *hd;
    seen_ = true;
  }
};

class ValueTesterInt32BoolMethodWithHandlerData : public Int32ValueTesterBase {
 public:
  typedef ValueTesterInt32BoolMethodWithHandlerData ME;
  void Register(upb::HandlersPtr h, upb::FieldDefPtr f) {
    ASSERT(h.SetInt32Handler(
        f, UpbBind(&ME::Handler, new int(kExpectedHandlerData))));
  }

 private:
  bool Handler(const int* hd, int32_t val) {
    val_ = val;
    handler_data_val_ = *hd;
    seen_ = true;
    return true;
  }
};

template <class T>
void RegisterHandlers(const void* closure, upb::Handlers* h_ptr) {
  T* tester = const_cast<T*>(static_cast<const T*>(closure));
  upb::HandlersPtr h(h_ptr);
  upb::FieldDefPtr f = h.message_def().FindFieldByNumber(T::kFieldNumber);
  ASSERT(f);
  tester->Register(h, f);
}

template <class T>
void TestHandler() {
  T tester;
  upb::SymbolTable symtab;
  upb::HandlerCache cache(&RegisterHandlers<T>, &tester);
  upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
  ASSERT(md);
  upb::FieldDefPtr f = md.FindFieldByNumber(T::kFieldNumber);
  ASSERT(f);

  const upb::Handlers* h = cache.Get(md);

  upb::Sink sink(h, &tester);
  tester.CallAndVerify(sink, f);
}

class T1 {};
class T2 {};

template <class C>
void DoNothingHandler(C* closure) {
  UPB_UNUSED(closure);
}

template <class C>
void DoNothingInt32Handler(C* closure, int32_t val) {
  UPB_UNUSED(closure);
  UPB_UNUSED(val);
}

template <class R>
class DoNothingStartHandler {
 public:
  // We wrap these functions inside of a class for a somewhat annoying reason.
  // UpbMakeHandler() is a macro, so we can't say
  //    UpbMakeHandler(DoNothingStartHandler<T1, T2>)
  //
  // because otherwise the preprocessor gets confused at the comma and tries to
  // make it two macro arguments.  The usual solution doesn't work either:
  //    UpbMakeHandler((DoNothingStartHandler<T1, T2>))
  //
  // If we do that the macro expands correctly, but then it tries to pass that
  // parenthesized expression as a template parameter, ie. Type<(F)>, which
  // isn't legal C++ (Clang will compile it but complains with
  //    warning: address non-type template argument cannot be surrounded by
  //    parentheses
  //
  // This two-level thing allows us to effectively pass two template parameters,
  // but without any commas:
  //    UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T2>)
  template <class C>
  static R* Handler(C* closure) {
    UPB_UNUSED(closure);
    return NULL;
  }

  template <class C>
  static R* String(C* closure, size_t size_len) {
    UPB_UNUSED(closure);
    UPB_UNUSED(size_len);
    return NULL;
  }
};

template <class C>
void DoNothingStringBufHandler(C* closure, const char *buf, size_t len) {
  UPB_UNUSED(closure);
  UPB_UNUSED(buf);
  UPB_UNUSED(len);
}

template <class C>
void DoNothingEndMessageHandler(C* closure, upb_status *status) {
  UPB_UNUSED(closure);
  UPB_UNUSED(status);
}

void RegisterMismatchedTypes(const void* closure, upb::Handlers* h_ptr) {
  upb::HandlersPtr h(h_ptr);

  upb::MessageDefPtr md(h.message_def());
  ASSERT(md);
  upb::FieldDefPtr i32 = md.FindFieldByName("i32");
  upb::FieldDefPtr r_i32 = md.FindFieldByName("r_i32");
  upb::FieldDefPtr str = md.FindFieldByName("str");
  upb::FieldDefPtr r_str = md.FindFieldByName("r_str");
  upb::FieldDefPtr msg = md.FindFieldByName("msg");
  upb::FieldDefPtr r_msg = md.FindFieldByName("r_msg");
  ASSERT(i32);
  ASSERT(r_i32);
  ASSERT(str);
  ASSERT(r_str);
  ASSERT(msg);
  ASSERT(r_msg);

  // Establish T1 as the top-level closure type.
  ASSERT(h.SetInt32Handler(i32, UpbMakeHandler(DoNothingInt32Handler<T1>)));

  // Now any other attempt to set another handler with T2 as the top-level
  // closure should fail.  But setting these same handlers with T1 as the
  // top-level closure will succeed.
  ASSERT(!h.SetStartMessageHandler(UpbMakeHandler(DoNothingHandler<T2>)));
  ASSERT(h.SetStartMessageHandler(UpbMakeHandler(DoNothingHandler<T1>)));

  ASSERT(
      !h.SetEndMessageHandler(UpbMakeHandler(DoNothingEndMessageHandler<T2>)));
  ASSERT(
      h.SetEndMessageHandler(UpbMakeHandler(DoNothingEndMessageHandler<T1>)));

  ASSERT(!h.SetStartStringHandler(
              str, UpbMakeHandler(DoNothingStartHandler<T1>::String<T2>)));
  ASSERT(h.SetStartStringHandler(
              str, UpbMakeHandler(DoNothingStartHandler<T1>::String<T1>)));

  ASSERT(!h.SetEndStringHandler(str, UpbMakeHandler(DoNothingHandler<T2>)));
  ASSERT(h.SetEndStringHandler(str, UpbMakeHandler(DoNothingHandler<T1>)));

  ASSERT(!h.SetStartSubMessageHandler(
              msg, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T2>)));
  ASSERT(h.SetStartSubMessageHandler(
              msg, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T1>)));

  ASSERT(
      !h.SetEndSubMessageHandler(msg, UpbMakeHandler(DoNothingHandler<T2>)));
  ASSERT(
      h.SetEndSubMessageHandler(msg, UpbMakeHandler(DoNothingHandler<T1>)));

  ASSERT(!h.SetStartSequenceHandler(
              r_i32, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T2>)));
  ASSERT(h.SetStartSequenceHandler(
              r_i32, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T1>)));

  ASSERT(!h.SetEndSequenceHandler(
              r_i32, UpbMakeHandler(DoNothingHandler<T2>)));
  ASSERT(h.SetEndSequenceHandler(
              r_i32, UpbMakeHandler(DoNothingHandler<T1>)));

  ASSERT(!h.SetStartSequenceHandler(
              r_msg, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T2>)));
  ASSERT(h.SetStartSequenceHandler(
              r_msg, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T1>)));

  ASSERT(!h.SetEndSequenceHandler(
              r_msg, UpbMakeHandler(DoNothingHandler<T2>)));
  ASSERT(h.SetEndSequenceHandler(
              r_msg, UpbMakeHandler(DoNothingHandler<T1>)));

  ASSERT(!h.SetStartSequenceHandler(
              r_str, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T2>)));
  ASSERT(h.SetStartSequenceHandler(
              r_str, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T1>)));

  ASSERT(!h.SetEndSequenceHandler(
              r_str, UpbMakeHandler(DoNothingHandler<T2>)));
  ASSERT(h.SetEndSequenceHandler(
              r_str, UpbMakeHandler(DoNothingHandler<T1>)));

  // By setting T1 as the return type for the Start* handlers we have
  // established T1 as the type of the sequence and string frames.
  // Setting callbacks that use T2 should fail, but T1 should succeed.
  ASSERT(
      !h.SetStringHandler(str, UpbMakeHandler(DoNothingStringBufHandler<T2>)));
  ASSERT(
      h.SetStringHandler(str, UpbMakeHandler(DoNothingStringBufHandler<T1>)));

  ASSERT(!h.SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler<T2>)));
  ASSERT(h.SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler<T1>)));

  ASSERT(!h.SetStartSubMessageHandler(
              r_msg, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T2>)));
  ASSERT(h.SetStartSubMessageHandler(
              r_msg, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T1>)));

  ASSERT(!h.SetEndSubMessageHandler(r_msg,
                                     UpbMakeHandler(DoNothingHandler<T2>)));
  ASSERT(h.SetEndSubMessageHandler(r_msg,
                                    UpbMakeHandler(DoNothingHandler<T1>)));

  ASSERT(!h.SetStartStringHandler(
              r_str, UpbMakeHandler(DoNothingStartHandler<T1>::String<T2>)));
  ASSERT(h.SetStartStringHandler(
              r_str, UpbMakeHandler(DoNothingStartHandler<T1>::String<T1>)));

  ASSERT(
      !h.SetEndStringHandler(r_str, UpbMakeHandler(DoNothingHandler<T2>)));
  ASSERT(h.SetEndStringHandler(r_str, UpbMakeHandler(DoNothingHandler<T1>)));

  ASSERT(!h.SetStringHandler(r_str,
                              UpbMakeHandler(DoNothingStringBufHandler<T2>)));
  ASSERT(h.SetStringHandler(r_str,
                             UpbMakeHandler(DoNothingStringBufHandler<T1>)));
}

void RegisterMismatchedTypes2(const void* closure, upb::Handlers* h_ptr) {
  upb::HandlersPtr h(h_ptr);

  upb::MessageDefPtr md(h.message_def());
  ASSERT(md);
  upb::FieldDefPtr i32 = md.FindFieldByName("i32");
  upb::FieldDefPtr r_i32 = md.FindFieldByName("r_i32");
  upb::FieldDefPtr str = md.FindFieldByName("str");
  upb::FieldDefPtr r_str = md.FindFieldByName("r_str");
  upb::FieldDefPtr msg = md.FindFieldByName("msg");
  upb::FieldDefPtr r_msg = md.FindFieldByName("r_msg");
  ASSERT(i32);
  ASSERT(r_i32);
  ASSERT(str);
  ASSERT(r_str);
  ASSERT(msg);
  ASSERT(r_msg);

  // For our second test we do the same in reverse.  We directly set the type of
  // the frame and then observe failures at registering a Start* handler that
  // returns a different type.

  // First establish the type of a sequence frame directly.
  ASSERT(h.SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler<T1>)));

  // Now setting a StartSequence callback that returns a different type should
  // fail.
  ASSERT(!h.SetStartSequenceHandler(
              r_i32, UpbMakeHandler(DoNothingStartHandler<T2>::Handler<T1>)));
  ASSERT(h.SetStartSequenceHandler(
              r_i32, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T1>)));

  // Establish a string frame directly.
  ASSERT(h.SetStringHandler(r_str,
                             UpbMakeHandler(DoNothingStringBufHandler<T1>)));

  // Fail setting a StartString callback that returns a different type.
  ASSERT(!h.SetStartStringHandler(
              r_str, UpbMakeHandler(DoNothingStartHandler<T2>::String<T1>)));
  ASSERT(h.SetStartStringHandler(
      r_str, UpbMakeHandler(DoNothingStartHandler<T1>::String<T1>)));

  // The previous established T1 as the frame for the r_str sequence.
  ASSERT(!h.SetStartSequenceHandler(
              r_str, UpbMakeHandler(DoNothingStartHandler<T2>::Handler<T1>)));
  ASSERT(h.SetStartSequenceHandler(
      r_str, UpbMakeHandler(DoNothingStartHandler<T1>::Handler<T1>)));
}

void TestMismatchedTypes() {
  // First create a schema for our test.
  upb::SymbolTable symtab;
  upb::HandlerCache handler_cache(&RegisterMismatchedTypes, nullptr);
  upb::HandlerCache handler_cache2(&RegisterMismatchedTypes2, nullptr);
  const upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));

  // Now test the type-checking in handler registration.
  handler_cache.Get(md);
  handler_cache2.Get(md);
}

class IntIncrementer {
 public:
  explicit IntIncrementer(int* x) : x_(x) { (*x_)++; }
  ~IntIncrementer() { (*x_)--; }

  static void Handler(void* closure, const IntIncrementer* incrementer,
                      int32_t x) {
    UPB_UNUSED(closure);
    UPB_UNUSED(incrementer);
    UPB_UNUSED(x);
  }

 private:
  int* x_;
};

void RegisterIncrementor(const void* closure, upb::Handlers* h_ptr) {
  const int* x = static_cast<const int*>(closure);
  upb::HandlersPtr h(h_ptr);
  upb::FieldDefPtr f = h.message_def().FindFieldByName("i32");
  h.SetInt32Handler(f, UpbBind(&IntIncrementer::Handler,
                               new IntIncrementer(const_cast<int*>(x))));
}

void TestHandlerDataDestruction() {
  int x = 0;

  {
    upb::SymbolTable symtab;
    upb::HandlerCache cache(&RegisterIncrementor, &x);
    upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
    cache.Get(md);
    ASSERT(x == 1);
  }

  ASSERT(x == 0);
}

void TestIteration() {
  upb::SymbolTable symtab;
  upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));

  // Test range-based for on both fields and oneofs (with the iterator adaptor).
  int field_count = 0;
  for (auto field : md.fields()) {
    UPB_UNUSED(field);
    field_count++;
  }
  ASSERT(field_count == md.field_count());

  int oneof_count = 0;
  for (auto oneof : md.oneofs()) {
    UPB_UNUSED(oneof);
    oneof_count++;
  }
  ASSERT(oneof_count == md.oneof_count());
}

extern "C" {

int run_tests(int argc, char *argv[]) {
  TestHandler<ValueTesterInt32VoidFunctionNoHandlerData>();
  TestHandler<ValueTesterInt32BoolFunctionNoHandlerData>();
  TestHandler<ValueTesterInt32VoidMethodNoHandlerData>();
  TestHandler<ValueTesterInt32BoolMethodNoHandlerData>();
  TestHandler<ValueTesterInt32VoidFunctionWithHandlerData>();
  TestHandler<ValueTesterInt32BoolFunctionWithHandlerData>();
  TestHandler<ValueTesterInt32VoidMethodWithHandlerData>();
  TestHandler<ValueTesterInt32BoolMethodWithHandlerData>();

  TestHandler<StartMsgTesterVoidFunctionNoHandlerData>();
  TestHandler<StartMsgTesterBoolFunctionNoHandlerData>();
  TestHandler<StartMsgTesterVoidMethodNoHandlerData>();
  TestHandler<StartMsgTesterBoolMethodNoHandlerData>();
  TestHandler<StartMsgTesterVoidFunctionWithHandlerData>();
  TestHandler<StartMsgTesterBoolFunctionWithHandlerData>();
  TestHandler<StartMsgTesterVoidMethodWithHandlerData>();
  TestHandler<StartMsgTesterBoolMethodWithHandlerData>();

  TestHandler<StringBufTesterVoidMethodNoHandlerDataNoHandle>();
  TestHandler<StringBufTesterVoidMethodNoHandlerDataWithHandle>();
  TestHandler<StringBufTesterVoidMethodWithHandlerDataNoHandle>();
  TestHandler<StringBufTesterVoidMethodWithHandlerDataWithHandle>();
  TestHandler<StringBufTesterVoidFunctionNoHandlerDataNoHandle>();
  TestHandler<StringBufTesterVoidFunctionNoHandlerDataWithHandle>();
  TestHandler<StringBufTesterVoidFunctionWithHandlerDataNoHandle>();
  TestHandler<StringBufTesterVoidFunctionWithHandlerDataWithHandle>();
  TestHandler<StringBufTesterSizeTMethodNoHandlerDataNoHandle>();
  TestHandler<StringBufTesterBoolMethodNoHandlerDataNoHandle>();

  TestMismatchedTypes();

  TestHandlerDataDestruction();
  TestIteration();

  return 0;
}

}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback