/** * Copyright - See the COPYRIGHT that is included with this distribution. * pvxs is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. */ #include #include #include #include #include #include namespace { using namespace pvxs; template void testEmpty() { testDiag("%s", __func__); shared_array v; testOk1(v.unique()); testOk1(v.empty()); testEq(v.size(), 0u); } template void testInt() { testDiag("%s w/ %s", __func__, typeid(I).name()); shared_array X(2, 5); testOk1(X.unique()); testOk1(!X.empty()); if(testEq(X.size(), 2u)) { testEq(X[0], 5); testEq(X[1], 5); } shared_array Y(X); testOk1(!X.unique()); testOk1(!Y.unique()); testEq(X.size(), Y.size()); X.clear(); testOk1(X.unique()); testOk1(Y.unique()); testEq(X.size(), 0u); testEq(Y.size(), 2u); X = std::move(Y); testOk1(X.unique()); testOk1(Y.unique()); testEq(X.size(), 2u); testEq(Y.size(), 0u); shared_array Z(std::move(X)); testOk1(X.unique()); testOk1(Y.unique()); testOk1(Z.unique()); testEq(X.size(), 0u); testEq(Y.size(), 0u); testEq(Z.size(), 2u); // copy empty shared_array Q(Y); testOk1(Y.unique()); testOk1(Q.unique()); testEq(Y.size(), 0u); testEq(Q.size(), 0u); } template void testVoid() { testDiag("%s", __func__); shared_array X(2); shared_array Y(X.template castTo()); testOk1(!X.unique()); testOk1(!Y.unique()); testEq(X.size(), 2u); testEq(Y.size(), 2u); testEq(Y.original_type(), ArrayType::UInt32); // never const uint32_t testThrows([&Y]() { auto Z = Y.freeze(); }); X.clear(); testOk1(Y.unique()); auto Z = Y.freeze(); testOk1(Y.unique()); testOk1(Z.unique()); testEq(Y.size(), 0u); testEq(Z.size(), 2u); } void testFreeze() { testDiag("%s", __func__); shared_array X(2, 5); shared_array Y(X.freeze()); testOk1(X.unique()); testOk1(Y.unique()); testEq(X.size(), 0u); testEq(Y.size(), 2u); } void testFreezeError() { testDiag("%s", __func__); shared_array X(2, 5), Z(X); testOk1(!X.unique()); testThrows([&X]() { shared_array Y(X.freeze()); })<<"Attempt to freeze() non-unique"; } void testComplex() { testDiag("%s", __func__); shared_array> X(2, nullptr); X[0] = decltype (X)::value_type{new uint32_t(4u)}; testEq(*X[0], 4u); } void testCast() { testDiag("%s", __func__); shared_array Void; (void)Void.castTo(); (void)Void.castTo(); // not allowed //(void)Void.castTo(); //(void)Void.castTo(); shared_array CVoid; (void)CVoid.castTo(); (void)CVoid.castTo(); // not allowed //(void)CVoid.castTo(); //(void)CVoid.castTo(); shared_array Int; (void)Int.castTo(); (void)Int.castTo(); // not allowed //(void)Int.castTo(); //(void)Int.castTo(); shared_array CInt; (void)CInt.castTo(); (void)CInt.castTo(); // not allowed //(void)CInt.castTo(); //(void)CInt.castTo(); shared_array Double({1.0, 2.0}); Void = Double.castTo(); testThrows([&Void](){ (void)Void.castTo(); })<<"Attempt cast to wrong type"; Void.clear(); // now doesn't throw (void)Void.castTo(); } void testFromVector() { testDiag("%s", __func__); std::vector V({1, 2, 3}); shared_array A(V.begin(), V.end()); testEq(A.size(), 3u); testEq(A.at(2), 3u); // not consumed testEq(V.size(), 3u); } void testElemAlloc() { testDiag("%s", __func__); testEq(elementSize(ArrayType::UInt8), 1u); testEq(elementSize(ArrayType::UInt16), 2u); testEq(elementSize(ArrayType::UInt32), 4u); testEq(elementSize(ArrayType::UInt64), 8u); auto varr = allocArray(ArrayType::UInt32, 3u); testEq(varr.size(), 3u); testEq(varr.original_type(), ArrayType::UInt32); } void testConvert() { testDiag("%s", __func__); static_assert (detail::CaptureCode::code!=detail::CaptureCode::code, ""); testArrEq(shared_array({1u, 2u, 0xffffffffu}).convertTo(), shared_array({1u, 2u, 0xffffffffu})); testArrEq(shared_array({1u, 2u, 0xffffffffu}).convertTo(), shared_array({1u, 2u, 0xffffu})); testArrEq(shared_array({1u, 2u, 0xffffffffu}).convertTo(), shared_array({1, 2, -1})); testArrEq(shared_array({1u, 2u, 0xffffffffu}).convertTo(), shared_array({1, 2, -1})); testArrEq(shared_array({1, 2, -1}).convertTo(), shared_array({1u, 2u, 0xffffffffu})); testArrEq(shared_array({1, 2, -1}).convertTo(), shared_array({1.0, 2.0, -1.0})); testArrEq(shared_array({1, 2, -1}).convertTo(), shared_array({"1", "2", "-1"})); } } // namespace MAIN(testshared) { testPlan(110); testSetup(); testEmpty(); testEmpty(); testEmpty(); testEmpty(); testInt(); testInt(); testVoid(); testVoid(); testFreeze(); testFreezeError(); testComplex(); testCast(); testFromVector(); testElemAlloc(); testConvert(); return testDone(); }