0027941: Foundation Classes - NCollection_Vector::Iterator::offsetV() does not match...
[occt.git] / src / QANCollection / QANCollection_Alloc.cxx
1 // Created on: 2004-03-05
2 // Created by: Mikhail KUZMITCHEV
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <QANCollection.hxx>
17 #include <Draw_Interpretor.hxx>
18
19 #include <NCollection_StdAllocator.hxx>
20 #include <NCollection_IncAllocator.hxx>
21 #include <Standard_Assert.hxx>
22
23 #include <list>
24 #include <vector>
25
26 //=======================================================================
27 //function : QANColStdAllocator1
28 //purpose  :
29 //=======================================================================
30 static Standard_Integer QANColStdAllocator1(Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
31 {
32   if ( argc != 1) {
33     di << "Usage : " << argv[0] << "\n";
34     return 1;
35   }
36
37   //type definitions
38   typedef Handle(Standard_Transient) elem_type;
39   typedef NCollection_StdAllocator<elem_type> allocator_type;
40   Standard_STATIC_ASSERT (sizeof (allocator_type::value_type) == sizeof (elem_type));
41   Standard_STATIC_ASSERT (sizeof (allocator_type::pointer) == sizeof (void*));
42   Standard_STATIC_ASSERT (sizeof (allocator_type::const_pointer) == sizeof (void*));
43
44   elem_type aDummy;
45   allocator_type::reference aRef = aDummy;
46   (void)aRef; // avoid compiler warning on unused
47   allocator_type::const_reference aConstRef = aDummy;
48   (void)aConstRef; // avoid compiler warning on unused
49   Standard_STATIC_ASSERT (sizeof (allocator_type::size_type) == sizeof (size_t));
50   Standard_STATIC_ASSERT (sizeof (allocator_type::difference_type) == sizeof (ptrdiff_t));
51
52   typedef int other_elem_type;
53   Standard_STATIC_ASSERT (sizeof (allocator_type::rebind<other_elem_type>::other::value_type) == sizeof (other_elem_type));
54
55   return 0;
56 }
57
58 //=======================================================================
59 //function : QANColStdAllocator2
60 //purpose  :
61 //=======================================================================
62 static Standard_Integer QANColStdAllocator2(Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
63 {
64   if ( argc != 1) {
65     di << "Usage : " << argv[0] << "\n";
66     return 1;
67   }
68
69   //create incremental allocator outside the scope of objects it will manage
70   Handle(NCollection_IncAllocator) anIncAlloc = new NCollection_IncAllocator();
71
72   {
73     //typed allocator
74     NCollection_StdAllocator<int> aSAlloc (anIncAlloc);
75     std::list<int, NCollection_StdAllocator<int> > aL (aSAlloc);
76     aL.push_back (2);
77     if ( aL.size() == size_t (1) ) {
78       di << "Test1 : OK\n";
79     } else {
80       di << "Test1 : Error\n";
81     }
82
83     //type cast
84     NCollection_StdAllocator<char> aCAlloc;
85     std::vector<int, NCollection_StdAllocator<int> > aV (aCAlloc);
86     aV.push_back (1);
87     if ( aV.size() == size_t (1) ) {
88       di << "Test2 : OK\n";
89     } else {
90       di << "Test2 : Error\n";
91     }
92
93     //using void-specialization allocator
94     NCollection_StdAllocator<void> aVAlloc;
95     std::vector<int, NCollection_StdAllocator<int> > aV2 (aVAlloc);
96
97     aV2.resize (10);
98     aV2.push_back (-1);
99     if ( aV2.size() == size_t (11) ) {
100       di << "Test3 : OK\n";
101     } else {
102       di << "Test3 : Error\n";
103     }
104
105     //equality of allocators
106     if ( aSAlloc != aCAlloc ) {
107       di << "Test4 : OK\n";
108     } else {
109       di << "Test4 : Error\n";
110     }
111     NCollection_StdAllocator<int> anIAlloc (anIncAlloc);
112     if ( aSAlloc == anIAlloc ) {
113       di << "Test5 : OK\n";
114     } else {
115       di << "Test5 : Error\n";
116     }
117
118   }
119
120   return 0;
121 }
122
123 void QANCollection::CommandsAlloc(Draw_Interpretor& theCommands) {
124   const char *group = "QANCollection";
125
126   theCommands.Add("QANColStdAllocator1", "QANColStdAllocator1", __FILE__, QANColStdAllocator1, group);
127   theCommands.Add("QANColStdAllocator2", "QANColStdAllocator2", __FILE__, QANColStdAllocator2, group);
128
129   return;
130 }