1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
10 #include "bitsetasuint64.h"
11 #include "bitsetasshortlong.h"
12 #include "bitsetasuint64inclass.h"
15 unsigned BitSetSupport::BitCountTable[16] = { 0, 1, 1, 2,
22 template <typename BitSetType, unsigned Uniq, typename Env, typename BitSetTraits>
23 void BitSetSupport::RunTests(Env env)
26 typedef BitSetOps<BitSetType, Uniq, Env, BitSetTraits> LclBitSetOps;
28 // The tests require that the Size is at least 52...
29 assert(BitSetTraits::GetSize(env) > 51);
32 LclBitSetOps::AssignNoCopy(env, bs1, LclBitSetOps::MakeEmpty(env));
33 unsigned bs1bits[] = {0, 10, 44, 45};
34 LclBitSetOps::AddElemD(env, bs1, bs1bits[0]);
35 LclBitSetOps::AddElemD(env, bs1, bs1bits[1]);
36 LclBitSetOps::AddElemD(env, bs1, bs1bits[2]);
37 LclBitSetOps::AddElemD(env, bs1, bs1bits[3]);
39 typename LclBitSetOps::Iter bsi(env, bs1);
42 while (bsi.NextElem(&bitNum))
44 assert(bitNum == bs1bits[k]);
49 assert(LclBitSetOps::Equal(env, bs1, LclBitSetOps::Union(env, bs1, bs1)));
50 assert(LclBitSetOps::Equal(env, bs1, LclBitSetOps::Intersection(env, bs1, bs1)));
51 assert(LclBitSetOps::IsSubset(env, bs1, bs1));
54 LclBitSetOps::AssignNoCopy(env, bs2, LclBitSetOps::MakeEmpty(env));
55 unsigned bs2bits[] = {0, 10, 50, 51};
56 LclBitSetOps::AddElemD(env, bs2, bs2bits[0]);
57 LclBitSetOps::AddElemD(env, bs2, bs2bits[1]);
58 LclBitSetOps::AddElemD(env, bs2, bs2bits[2]);
59 LclBitSetOps::AddElemD(env, bs2, bs2bits[3]);
61 unsigned unionBits[] = {0, 10, 44, 45, 50, 51};
63 LclBitSetOps::AssignNoCopy(env, bsU12, LclBitSetOps::Union(env, bs1, bs2));
65 bsi = typename LclBitSetOps::Iter(env, bsU12);
67 while (bsi.NextElem(&bitNum))
69 assert(bitNum == unionBits[k]);
75 typename LclBitSetOps::Iter bsiL = typename LclBitSetOps::Iter(env, bsU12);
77 while (bsiL.NextElem(&bitNum))
79 assert(bitNum == unionBits[k]);
84 unsigned intersectionBits[] = {0, 10};
86 LclBitSetOps::AssignNoCopy(env, bsI12, LclBitSetOps::Intersection(env, bs1, bs2));
88 bsi = typename LclBitSetOps::Iter(env, bsI12);
90 while (bsi.NextElem(&bitNum))
92 assert(bitNum == intersectionBits[k]);
98 class TestBitSetTraits
101 static void* Alloc(CompAllocator* alloc, size_t byteSize)
103 return alloc->Alloc(byteSize);
105 static unsigned GetSize(CompAllocator* alloc)
109 static unsigned GetArrSize(CompAllocator* alloc, unsigned elemSize)
111 assert(elemSize == sizeof(size_t));
112 return (64 / 8) / sizeof(size_t);
114 static unsigned GetEpoch(CompAllocator* alloc)
120 void BitSetSupport::TestSuite(CompAllocator* env)
122 BitSetSupport::RunTests<UINT64, BSUInt64, CompAllocator*, TestBitSetTraits>(env);
123 BitSetSupport::RunTests<BitSetShortLongRep, BSShortLong, CompAllocator*, TestBitSetTraits>(env);
124 BitSetSupport::RunTests<BitSetUint64<CompAllocator*, TestBitSetTraits>, BSUInt64Class, CompAllocator*,
125 TestBitSetTraits>(env);
129 const char* BitSetSupport::OpNames[BitSetSupport::BSOP_NUMOPS] = {
130 #define BSOPNAME(x) #x,
131 #include "bitsetops.h"
135 void BitSetSupport::BitSetOpCounter::RecordOp(BitSetSupport::Operation op)
140 if ((TotalOps % 1000000) == 0)
142 if (OpOutputFile == nullptr)
144 OpOutputFile = fopen(m_fileName, "a");
146 fprintf(OpOutputFile, "@ %d total ops.\n", TotalOps);
148 unsigned OpOrder[BSOP_NUMOPS];
149 bool OpOrdered[BSOP_NUMOPS];
151 // First sort by total operations (into an index permutation array, using a simple n^2 sort).
152 for (unsigned k = 0; k < BitSetSupport::BSOP_NUMOPS; k++)
154 OpOrdered[k] = false;
156 for (unsigned k = 0; k < BitSetSupport::BSOP_NUMOPS; k++)
158 bool candSet = false;
160 unsigned candInd = 0;
161 for (unsigned j = 0; j < BitSetSupport::BSOP_NUMOPS; j++)
167 if (!candSet || OpCounts[j] > cand)
175 OpOrder[k] = candInd;
176 OpOrdered[candInd] = true;
179 for (unsigned ii = 0; ii < BitSetSupport::BSOP_NUMOPS; ii++)
181 unsigned i = OpOrder[ii];
182 fprintf(OpOutputFile, " Op %40s: %8d\n", OpNames[i], OpCounts[i]);