⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 static_array_unittest_.h

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 H
📖 第 1 页 / 共 2 页
字号:
            // Test the range-validation

            // DIM0
            try
            {
                ar.at(DIM0);

                r->report("3d: ar.at() out of range failed to throw", __LINE__);
                bSuccess = false;
            }
            catch(::std::out_of_range &)
            {}

            try
            {
                ar.at(DIM0, 0, 0);

                r->report("3d: ar.at(,,,) out of range failed to throw", __LINE__);
                bSuccess = false;
            }
            catch(::std::out_of_range &)
            {}

            // DIM1
            try
            {
                ar.at(0, DIM1, 0);

                r->report("3d: ar.at(,,,) out of range failed to throw", __LINE__);
                bSuccess = false;
            }
            catch(::std::out_of_range &)
            {}

            // DIM2
            try
            {
                ar.at(0, 0, DIM2);

                r->report("3d: ar.at(,,,) out of range failed to throw", __LINE__);
                bSuccess = false;
            }
            catch(::std::out_of_range &)
            {}

            return bSuccess;
        }

        template <ss_typename_param_k T>
        ss_bool_t test_2d(unittest_reporter *r)
        {
            const ss_size_t         DIM0        =   2;
            const ss_size_t         DIM1        =   3;
            ss_bool_t               bSuccess    =   true;

            typedef static_array_2d<T, DIM0, DIM1>  array_2d_t;

            array_2d_t  ar;

            if(DIM0 != ar.dimension0())
            {
                r->report("2d: dim0 invalid", __LINE__);
                bSuccess = false;
            }

            if(DIM1 != ar.dimension1())
            {
                r->report("2d: dim1 invalid", __LINE__);
                bSuccess = false;
            }

# if !defined(STLSOFT_COMPILER_IS_MSVC) || \
     _MSC_VER >= 1200
            if(array_size(ar) != ar.size())
            {
                r->report("2d: array_size() != ar.size()", __LINE__);
                bSuccess = false;
            }
#endif /* compiler */

            // Initialise the array contents
            T   total = 0;
            T   totals0[DIM0];

            stlsoft_ns_qual_std(fill_n)(&totals0[0], STLSOFT_NUM_ELEMENTS(totals0), 0);

            { for(ss_size_t i0 = 0; i0 < ar.dimension0(); ++i0)
            {
                { for(ss_size_t i1 = 0; i1 < ar.dimension1(); ++i1)
                {
                    T   value       =   static_cast<T>(0)
                                    +   static_cast<T>(i1)
                                    +   static_cast<T>(i0 * DIM1)
                                    ;

                    ar[i0][i1]      =   value;
                    total           +=  value;
                    totals0[i0]     +=  value;
                }}
            }}

            // Now do a accumulate
            const T total2  =   stlsoft_ns_qual_std(accumulate)(ar.begin(), ar.end(), T());

            if(total != total2)
            {
                r->report("2d: accumulate(ar.begin(), ar,begin(), T()) != initialisation total", __LINE__);
                bSuccess = false;
            }

            { for(ss_size_t i0 = 0; i0 < ar.dimension0(); ++i0)
            {
                const T totalN  =   stlsoft_ns_qual_std(accumulate)(ar[i0].begin(), ar[i0].end(), T());

                if(totals0[i0] != totalN)
                {
                    r->report("2d: accumulate(ar.begin(), ar,begin(), T()) != initialisation total", __LINE__);
                    bSuccess = false;
                }

            }}

            // Test the range-validation

            // DIM0
            try
            {
                ar.at(DIM0);

                r->report("2d: ar.at() out of range failed to throw", __LINE__);
                bSuccess = false;
            }
            catch(::std::out_of_range &)
            {}

            try
            {
                ar.at(DIM0, 0);

                r->report("2d: ar.at(,,,) out of range failed to throw", __LINE__);
                bSuccess = false;
            }
            catch(::std::out_of_range &)
            {}

            // DIM1
            try
            {
                ar.at(0, DIM1);

                r->report("2d: ar.at(,,,) out of range failed to throw", __LINE__);
                bSuccess = false;
            }
            catch(::std::out_of_range &)
            {}

            return bSuccess;
        }

        template <ss_typename_param_k T>
        ss_bool_t test_1d(unittest_reporter *r)
        {
            const ss_size_t         DIM0    =   2;
            ss_bool_t               bSuccess    =   true;

            typedef static_array_1d<T, DIM0>  array_1d_t;

            array_1d_t  ar;

            if(DIM0 != ar.dimension0())
            {
                r->report("1d: dim0 invalid", __LINE__);
                bSuccess = false;
            }

# if !defined(STLSOFT_COMPILER_IS_MSVC) || \
     _MSC_VER >= 1200
            if(array_size(ar) != ar.size())
            {
                r->report("1d: array_size() != ar.size()", __LINE__);
                bSuccess = false;
            }
#endif /* compiler */

            // Initialise the array contents
            T   total = 0;

            { for(ss_size_t i0 = 0; i0 < ar.dimension0(); ++i0)
            {
                T   value   =   static_cast<T>(0)
                            +   static_cast<T>(i0)
                            ;

                ar[i0]      =   value;
                total       +=  value;
            }}

            // Now do a accumulate
            const T total2  =   stlsoft_ns_qual_std(accumulate)(ar.begin(), ar.end(), T());

            if(total != total2)
            {
                r->report("1d: accumulate(ar.begin(), ar,begin(), T()) != initialisation total", __LINE__);
                bSuccess = false;
            }

            // Test the range-validation

            // DIM0
            try
            {
                ar.at(DIM0);

                r->report("1d: ar.at() out of range failed to throw", __LINE__);
                bSuccess = false;
            }
            catch(::std::out_of_range &)
            {}

            return bSuccess;
        }

        ss_bool_t test_stlsoft_containers_static_array(unittest_reporter *r)
        {
            ss_bool_t               bSuccess    =   true;

            unittest_initialiser    init(r, "STLSoft", "containers/static_array", __FILE__);

            if(bSuccess)
            {
                bSuccess = test_4d<int>(r);
            }

            if(bSuccess)
            {
                bSuccess = test_3d<int>(r);
            }

            if(bSuccess)
            {
                bSuccess = test_2d<int>(r);
            }

            if(bSuccess)
            {
                bSuccess = test_1d<int>(r);
            }

            return bSuccess;
        }

        unittest_registrar    unittest_stlsoft_containers_static_array(test_stlsoft_containers_static_array);
    } // anonymous namespace

} // namespace unittest

# endif /* compiler */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -