CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
compare.h
1
2#ifndef _aspose_system_test_tools_compare_h_
3#define _aspose_system_test_tools_compare_h_
4
5#include "system/array.h"
6#include "system/collections/list.h"
7#include "system/collections/dictionary.h"
8#include "system/collections/hashset.h"
9#include "system/collections/queue.h"
10#include "system/collections/stack.h"
11#include "system/collections/sorted_dictionary.h"
12#include "system/collections/sorted_list.h"
13#include "system/collections/bitarray.h"
14#include "system/collections/ienumerable.h"
15#include "system/collections/specialized/string_collection.h"
16
17#include <system/boxed_value.h>
18
19#include <system/date_time.h>
20#include <system/timespan.h>
21#include <system/decimal.h>
22#include <system/guid.h>
23
24#include <algorithm>
25
27
28namespace System {
29
32{
33 static ASPOSECPP_SHARED_API double& GetDefaultFloatingPointTolerance();
34
35 template <class...>
36 using void_t = void;
37
38 template <typename T, typename = void>
39 struct IsEnumerable : std::false_type
40 {
41 using EnumeratorType = std::nullptr_t;
42 };
43
44 template <typename T>
45 struct IsEnumerable<T, void_t<typename T::ValueType, std::enable_if_t<std::is_base_of<
47 : std::true_type
48 {
49 using ValueType = typename T::ValueType;
51 };
52
53 template <typename X>
54 struct IsEnumerable<System::SharedPtr<X>, void_t<typename X::ValueType, std::enable_if_t<std::is_base_of<
55 System::Collections::Generic::IEnumerable<typename X::ValueType>, X>::value>>>
56 : std::true_type
57 {
58 using ValueType = typename X::ValueType;
60 };
61
62 template <typename T>
63 static std::enable_if_t<IsEnumerable<T>::value, typename IsEnumerable<T>::EnumeratorType> GetEnumeratorOrNull(const T& obj)
64 {
65 return obj->GetEnumerator();
66 }
67
68 template <typename T>
69 static std::enable_if_t<!IsEnumerable<T>::value, System::Collections::Generic::IEnumerator<int>*>
71 {
72 return nullptr;
73 }
74
75 // Array
82 template <typename T, typename U>
83 static bool AreEqual(const SharedPtr<Array<T>>& arrA, const SharedPtr<Array<U>>& arrB)
84 {
85 return AreEqualData(arrA, arrB);
86 }
93 template <typename T, typename U>
94 static bool AreEqual(const SharedPtr<Array<SharedPtr<T>>>& arrA,
95 const SharedPtr<Array<SharedPtr<U>>>& arrB)
96 {
97 if ((void*)arrA.GetObjectOrNull() == (void*)arrB.GetObjectOrNull())
98 return true;
99
100 if (!arrA || !arrB)
101 return false;
102
103 return AreEqualContainerOfObjects(arrA->data(), arrB->data());
104 }
105
106 // List
113 template <typename T, typename U>
114 static bool AreEqual(const SharedPtr< SCG::List<T> >& listA, const SharedPtr< SCG::List<U> >& listB)
115 {
116 return AreEqualData(listA, listB);
117 }
124 template <typename T, typename U>
125 static bool AreEqual(const SharedPtr< SCG::List< SharedPtr<T> > >& listA,
126 const SharedPtr< SCG::List< SharedPtr<U> > >& listB)
127 {
128 if ((void*)listA.GetObjectOrNull() == (void*)listB.GetObjectOrNull())
129 return true;
130
131 if (!listA || !listB)
132 return false;
133
134 return AreEqualContainerOfObjects(listA->data(), listB->data());
135 }
136
137 // List with Array
144 template <typename T, typename U>
145 static bool AreEqual(const SharedPtr< SCG::List<T> >& listA, const System::ArrayPtr<U>& arrB)
146 {
147 return AreEqualData(listA, arrB);
148 }
155 template <typename T, typename U>
156 static bool AreEqual(const System::ArrayPtr<T>& arrA, const SharedPtr< SCG::List<U> >& listB)
157 {
158 return AreEqualData(arrA, listB);
159 }
166 template <typename T, typename U>
167 static bool AreEqual(const System::ArrayPtr< SharedPtr<T> >& arr,
168 const SharedPtr< SCG::List< SharedPtr<U> > >& list)
169 {
170 if ((void*)arr.GetObjectOrNull() == (void*)list.GetObjectOrNull())
171 return true;
172
173 if (!arr || !list)
174 return false;
175
176 return AreEqualContainerOfObjects(arr->data(), list->data());
177 }
184 template <typename T, typename U>
185 static bool AreEqual(const SharedPtr< SCG::List< SharedPtr<T> > >& list,
186 const System::ArrayPtr< SharedPtr<U> >& arr)
187 {
188 if ((void*)list.GetObjectOrNull() == (void*)arr.GetObjectOrNull())
189 return true;
190
191 if (!list || !arr)
192 return false;
193
194 return AreEqualContainerOfObjects(list->data(), arr->data());
195 }
196
197 // Ditionary
204 template <typename K, typename U>
205 static bool AreEqual(const SharedPtr< SCG::Dictionary<K, U> >& dictA, const SharedPtr< SCG::Dictionary<K, U> >& dictB)
206 {
207 return AreEqualData(dictA, dictB);
208 }
215 template <typename K, typename U>
216 static bool AreEqual(const SharedPtr< SCG::Dictionary<K, SharedPtr<U> > >& dictA,
217 const SharedPtr< SCG::Dictionary<K, SharedPtr<U> > >& dictB)
218 {
219 if ((void*)dictA.GetObjectOrNull() == (void*)dictB.GetObjectOrNull())
220 return true;
221
222 if (!dictA || !dictB)
223 return false;
224
225 return AreEqualMapOfObjects(dictA->data(), dictB->data());
226 }
235 template <typename K1, typename U1, typename K2, typename U2>
236 static bool AreEqual(const SharedPtr< SCG::Dictionary<K1, U1> >& dictA, const SharedPtr< SCG::Dictionary<K2, U2> >& dictB)
237 {
238 ASPOSE_UNUSED(dictA);
239 ASPOSE_UNUSED(dictB);
240 return false;
241 }
242
243 // HashSet
250 template <typename T, typename U>
251 static bool AreEqual(const SharedPtr< SCG::HashSet<T> >& containerPtrA, const SharedPtr< SCG::HashSet<U> >& containerPtrB)
252 {
253 if ((void*)containerPtrA.GetObjectOrNull() == (void*)containerPtrB.GetObjectOrNull())
254 return true;
255
256 if (!containerPtrA || !containerPtrB)
257 return false;
258
259 if (containerPtrA->data().size() != containerPtrB->data().size())
260 return false;
261
262 return std::equal(containerPtrA->data().begin(), containerPtrA->data().end(), containerPtrB->data().begin());
263 }
270 template <typename T, typename U>
271 static bool AreEqual(const SharedPtr< SCG::HashSet< SharedPtr<T> > >& contA,
272 const SharedPtr< SCG::HashSet< SharedPtr<U> > >& contB)
273 {
274 if ((void*)contA.GetObjectOrNull() == (void*)contB.GetObjectOrNull())
275 return true;
276
277 if (!contA || !contB)
278 return false;
279
280 return AreEqualContainerOfObjects(contA->data(), contB->data());
281 }
282
283 // Queue
290 template <typename T, typename U>
291 static bool AreEqual(const SCG::QueuePtr<T>& queueA, const SCG::QueuePtr<U>& queueB)
292 {
293 return AreEqualData(queueA, queueB);
294 }
301 template <typename T, typename U>
302 static bool AreEqual(const SCG::QueuePtr< SharedPtr<T> >& queueA,
303 const SCG::QueuePtr< SharedPtr<U> >& queueB)
304 {
305 if ((void*)queueA.GetObjectOrNull() == (void*)queueB.GetObjectOrNull())
306 return true;
307
308 if (!queueA || !queueB)
309 return false;
310
311 return AreEqualContainerOfObjects(queueA->data(), queueB->data());
312 }
313
314 // Stack
321 template <typename T, typename U>
322 static bool AreEqual(const SharedPtr< SCG::Stack<T> >& stackA, const SharedPtr< SCG::Stack<U> >& stackB)
323 {
324 return AreEqualData(stackA, stackB);
325 }
332 template <typename T, typename U>
333 static bool AreEqual(const SharedPtr< SCG::Stack< SharedPtr<T> > >& stackA,
334 const SharedPtr< SCG::Stack< SharedPtr<U> > >& stackB)
335 {
336 if ((void*)stackA.GetObjectOrNull() == (void*)stackB.GetObjectOrNull())
337 return true;
338
339 if (!stackA || !stackB)
340 return false;
341
342 return AreEqualContainerOfObjects(stackA->data(), stackB->data());
343 }
344
345 // SortedDitionary
352 template <typename K, typename U>
354 {
355 return AreEqualData(dictA, dictB);
356 }
363 template <typename K, typename U>
364 static bool AreEqual(const SharedPtr<SCG::SortedDictionary<K, SharedPtr<U> >>& dictA,
366 {
367 if ((void*)dictA.GetObjectOrNull() == (void*)dictB.GetObjectOrNull())
368 return true;
369
370 if (!dictA || !dictB)
371 return false;
372
373 return AreEqualMapOfObjects(dictA->data(), dictB->data());
374 }
383 template <typename K1, typename U1, typename K2, typename U2>
385 {
386 return false;
387 }
388
389 // SortedList
396 template <typename K, typename U>
398 {
399 return AreEqualData(dictA, dictB);
400 }
407 template <typename K, typename U>
408 static bool AreEqual(const SharedPtr<SCG::SortedList<K, SharedPtr<U> >>& dictA,
409 const SharedPtr<SCG::SortedList<K, SharedPtr<U> >>& dictB)
410 {
411 if ((void*)dictA.GetObjectOrNull() == (void*)dictB.GetObjectOrNull())
412 return true;
413
414 if (!dictA || !dictB)
415 return false;
416
417 return AreEqualMapOfObjects(dictA->data(), dictB->data());
418 }
427 template <typename K1, typename U1, typename K2, typename U2>
429 {
430 return false;
431 }
432
433 // StringCollection
439 {
440 return AreEqualData(arrA, arrB);
441 }
442
443 // IEnumerable
448 template <typename T, typename U>
450 {
451 auto tenum = et->GetEnumerator();
452 auto uenum = eu->GetEnumerator();
453
454 while (tenum->MoveNext())
455 {
456 if (!uenum->MoveNext())
457 {
458 return false;
459 }
460 if (!AreEqualObjects(tenum->get_Current(), uenum->get_Current()))
461 {
462 return false;
463 }
464 }
465 return !uenum->MoveNext();
466 }
467
468 // ICollection
474 template <typename T>
475 static bool AbstractEqual(SCG::ICollection<T>* const collA, SCG::ICollection<T>* const collB)
476 {
477 static_assert(System::IsBoxable<T>::value, "Unsupported type for ICollection<T,_> compare");
478
479 if (collA == collB)
480 return true;
481
482 if (!collA || !collB)
483 return false;
484
485 if (collA->get_Count() != collB->get_Count())
486 return false;
487
488 if (collA->get_Count() == 0)
489 return true;
490
491 auto enA = collA->GetEnumerator();
492 auto enB = collB->GetEnumerator();
493
494 while (enA->MoveNext() && enB->MoveNext())
495 {
496 auto&& itA = enA->get_Current();
497 auto&& itB = enB->get_Current();
498 if (itA != itB)
499 return false;
500 }
501
502 return true;
503 }
504
505private:
506 // test containers of primitive type
512 template <typename T>
513 static bool AreEqualData(const SharedPtr<T>& containerPtrA,
514 const SharedPtr<T>& containerPtrB)
515 {
516 if (containerPtrA == containerPtrB)
517 return true;
518
519 if (!containerPtrA || !containerPtrB)
520 return false;
521
522 return containerPtrA->data() == containerPtrB->data();
523 }
530 template <typename T, typename U>
531 static bool AreEqualData(const SharedPtr<T>& containerPtrA,
532 const SharedPtr<U>& containerPtrB)
533 {
534 if ((void*)containerPtrA.GetObjectOrNull() == (void*)containerPtrB.GetObjectOrNull())
535 return true;
536
537 if (!containerPtrA || !containerPtrB)
538 return false;
539
540 if (containerPtrA->data().size() != containerPtrB->data().size())
541 return false;
542
543 return std::equal(containerPtrA->data().begin(), containerPtrA->data().end(), containerPtrB->data().begin());
544 }
545
546 // test containers of SharedPtr<T>
553 template <typename T, typename U>
554 static bool AreEqualObjects(const T& ptrA, const U& ptrB)
555 {
556 // Try to get Enumerator
557 auto enA = GetEnumeratorOrNull(ptrA);
558 auto enB = GetEnumeratorOrNull(ptrB);
559 if (enA && enB)
560 {
561 auto more = false;
562 while ((more = enA->MoveNext()) == enB->MoveNext())
563 {
564 if (!more)
565 {
566 return true;
567 }
568 else if (!AreEqualObjects(enA->get_Current(), enB->get_Current()))
569 {
570 break;
571 }
572 }
573 }
574
575 return System::Object::Equals(ptrA, ptrB);
576 }
583 template <typename K, typename V>
584 static bool AreEqualObjectPairs(const std::pair<K, V>& pairA, const std::pair<K, V>& pairB)
585 {
586 return pairA.first == pairB.first && System::Object::Equals(pairA.second, pairB.second);
587 }
594 template <typename Tvect, typename Uvect>
595 static bool AreEqualContainerOfObjects(Tvect& contA, Uvect& contB)
596 {
597 if (contA.size() != contB.size())
598 return false;
599
600 return std::equal(contA.begin(), contA.end(), contB.begin(),
601 TestCompare::AreEqualObjects<typename Tvect::value_type, typename Uvect::value_type>);
602 }
608 template <typename Tmap>
609 static bool AreEqualMapOfObjects(const Tmap& mapA, const Tmap& mapB)
610 {
611 if (mapA.size() != mapB.size())
612 return false;
613
614#if 0
615 // for ordered map
616 return std::equal(mapA.begin(), mapA.end(), mapB.begin(),
617 TestCompare::AreEqualObjectPairs<Tmap::key_type, Tmap::mapped_type>);
618#else
619 // for ordered and unordered maps
620
621 for (typename Tmap::const_iterator itA = mapA.begin(); itA != mapA.end(); ++itA)
622 {
623 typename Tmap::const_iterator itF = mapB.find(itA->first);
624 if (itF == mapB.end() || !itF->second->Equals(itA->second))
625 return false;
626 }
627
628 return true;
629 }
630#endif
631
632}; // struct TestCompare
633
634namespace CompareDetails {
635
636template <typename X, typename Y>
637bool AreEqual(const X& x, const Y& y)
638{
639 // Try to get Enumerator
642 if (enX && enY)
643 {
644 auto more = false;
645 while ((more = enX->MoveNext()) == enY->MoveNext())
646 {
647 if (!more)
648 {
649 return true;
650 }
651 else if (!CompareDetails::AreEqual(enX->get_Current(), enY->get_Current()))
652 {
653 break;
654 }
655 }
656 }
657
658 return System::Details::AreEqual(x, y);
659}
660
661}
662
663} // namespace System
664
665#endif // _aspose_system_test_tools_compare_h_
Class that represents an array data structure. Objects of this class should only be allocated using S...
Definition: array.h:296
Forward declaration of Dictionary class.
Definition: dictionary.h:96
Forward declaration of HashSet class.
Definition: hashset.h:82
Interface of collection of elements. Objects of this class should only be allocated using System::Mak...
Definition: icollection.h:20
virtual int get_Count() const =0
Gets number of elements in collection.
Interface of object providing enumerator on contained elements.
Definition: ienumerable.h:26
virtual SharedPtr< IEnumerator< T > > GetEnumerator()=0
Gets enumerator.
Interface of enumerator which can be used to iterate through some elements. Objects of this class sho...
Definition: ienumerator.h:63
List forward declaration.
Definition: list.h:129
Queue pointer. This type is a pointer to manage other object's deletion. It should be allocated on st...
Definition: queue.h:26
Sorted dictionary type forward declaration.
Definition: sorted_dictionary.h:52
Sorted list wrapping FlatMap structure. Objects of this class should only be allocated using System::...
Definition: sorted_list.h:334
Stack class forward declaration.
Definition: stack.h:87
Stirng collection poiner with access operator.
Definition: string_collection.h:195
virtual bool Equals(ptr obj)
Compares objects using C# Object.Equals semantics.
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Baseclass for value types with Object inheritance being truncated for performance reasons....
Definition: value_type.h:17
Definition: algorithms.h:10
bool AreEqual(const X &x, const Y &y)
Definition: compare.h:637
Definition: db_command.h:9
Template predicate that checks if boxing of the specified type is supported.
Definition: boxable_traits.h:16
Definition: compare.h:40
std::nullptr_t EnumeratorType
Definition: compare.h:41
Service structure providing interface to compare collections.
Definition: compare.h:32
static bool AreEqual(const SharedPtr< Array< T > > &arrA, const SharedPtr< Array< U > > &arrB)
Compares arrays of non-pointers.
Definition: compare.h:83
void void_t
Definition: compare.h:36
static bool AreEqual(const SCG::QueuePtr< T > &queueA, const SCG::QueuePtr< U > &queueB)
Compares queues of non-pointers.
Definition: compare.h:291
static bool AreEqual(const SharedPtr< SCG::List< SharedPtr< T > > > &listA, const SharedPtr< SCG::List< SharedPtr< U > > > &listB)
Compares lists of pointers.
Definition: compare.h:125
static bool AreEqual(const SharedPtr< SCG::SortedList< K, SharedPtr< U > > > &dictA, const SharedPtr< SCG::SortedList< K, SharedPtr< U > > > &dictB)
Compares sorted lists of pointer mapped types.
Definition: compare.h:408
static bool AreEqual(const SharedPtr< SCG::SortedList< K1, U1 > > &dictA, const SharedPtr< SCG::SortedList< K2, U2 > > &dictB)
Compares sorted lists of different types.
Definition: compare.h:428
static bool AreEqual(const System::ArrayPtr< T > &arrA, const SharedPtr< SCG::List< U > > &listB)
Compares lists with arrays in non-pointers elements case.
Definition: compare.h:156
static bool AreEqual(const SharedPtr< SCG::SortedList< K, U > > &dictA, const SharedPtr< SCG::SortedList< K, U > > &dictB)
Compares sorted lists of non-pointer mapped types.
Definition: compare.h:397
static bool AreEqual(const System::Collections::Specialized::StringCollectionPtr &arrA, const System::Collections::Specialized::StringCollectionPtr &arrB)
Compares string collections.
Definition: compare.h:438
static bool AreEqual(const SCG::QueuePtr< SharedPtr< T > > &queueA, const SCG::QueuePtr< SharedPtr< U > > &queueB)
Compares queues of pointers.
Definition: compare.h:302
static std::enable_if_t< IsEnumerable< T >::value, typename IsEnumerable< T >::EnumeratorType > GetEnumeratorOrNull(const T &obj)
Definition: compare.h:63
static bool AreEqual(const SharedPtr< SCG::List< T > > &listA, const SharedPtr< SCG::List< U > > &listB)
Compares lists of non-pointers.
Definition: compare.h:114
static bool AreEqual(const SharedPtr< SCG::HashSet< T > > &containerPtrA, const SharedPtr< SCG::HashSet< U > > &containerPtrB)
Compares hashsets of non-pointers.
Definition: compare.h:251
static bool AreEqual(const SharedPtr< SCG::List< T > > &listA, const System::ArrayPtr< U > &arrB)
Compares lists with arrays in non-pointers elements case.
Definition: compare.h:145
static bool AreEqual(const SharedPtr< SCG::SortedDictionary< K, U > > &dictA, const SharedPtr< SCG::SortedDictionary< K, U > > &dictB)
Compares sorted dictionaries of non-pointer mapped types.
Definition: compare.h:353
static bool AreEqual(const SharedPtr< Array< SharedPtr< T > > > &arrA, const SharedPtr< Array< SharedPtr< U > > > &arrB)
Compares arrays of pointers.
Definition: compare.h:94
static bool AreEqual(const SharedPtr< SCG::Stack< T > > &stackA, const SharedPtr< SCG::Stack< U > > &stackB)
Compares stacks of non-pointers.
Definition: compare.h:322
static bool AreEqual(const SharedPtr< SCG::Dictionary< K, U > > &dictA, const SharedPtr< SCG::Dictionary< K, U > > &dictB)
Compares dictionaries of non-pointer mapped types.
Definition: compare.h:205
static double & GetDefaultFloatingPointTolerance()
static bool AreEqual(const SharedPtr< SCG::SortedDictionary< K1, U1 > > &dictA, const SharedPtr< SCG::SortedDictionary< K2, U2 > > &dictB)
Compares sorted dictionaries of different types.
Definition: compare.h:384
static bool AreEqual(const System::ArrayPtr< SharedPtr< T > > &arr, const SharedPtr< SCG::List< SharedPtr< U > > > &list)
Compares lists with arrays in pointers elements case.
Definition: compare.h:167
static bool AreEqual(const SharedPtr< SCG::SortedDictionary< K, SharedPtr< U > > > &dictA, const SharedPtr< SCG::SortedDictionary< K, SharedPtr< U > > > &dictB)
Compares sorted dictionaries of pointer mapped types.
Definition: compare.h:364
static bool AreEqual(const SharedPtr< SCG::HashSet< SharedPtr< T > > > &contA, const SharedPtr< SCG::HashSet< SharedPtr< U > > > &contB)
Compares hashsets of pointers.
Definition: compare.h:271
static std::enable_if_t<!IsEnumerable< T >::value, System::Collections::Generic::IEnumerator< int > * > GetEnumeratorOrNull(const T &)
Definition: compare.h:70
static bool AreEqual(const SharedPtr< SCG::Stack< SharedPtr< T > > > &stackA, const SharedPtr< SCG::Stack< SharedPtr< U > > > &stackB)
Compares stacks of pointers.
Definition: compare.h:333
static bool AbstractEqual(SCG::ICollection< T > *const collA, SCG::ICollection< T > *const collB)
Compares two collections of unknown type.
Definition: compare.h:475
static bool AreEqual(const SharedPtr< SCG::Dictionary< K1, U1 > > &dictA, const SharedPtr< SCG::Dictionary< K2, U2 > > &dictB)
Compares dictionaries of different types.
Definition: compare.h:236
static bool AreEqual(const System::SharedPtr< SCG::IEnumerable< T > > &et, const System::SharedPtr< SCG::IEnumerable< U > > &eu)
Compares IEnumerable instances.
Definition: compare.h:449
static bool AreEqual(const SharedPtr< SCG::List< SharedPtr< T > > > &list, const System::ArrayPtr< SharedPtr< U > > &arr)
Compares lists with arrays in pointers elements case.
Definition: compare.h:185
static bool AreEqual(const SharedPtr< SCG::Dictionary< K, SharedPtr< U > > > &dictA, const SharedPtr< SCG::Dictionary< K, SharedPtr< U > > > &dictB)
Compares dictionaries of pointer mapped types.
Definition: compare.h:216