Exemplo n.º 1
0
static int
run_test (int, char**)
{
    MyIos<char, std::char_traits<char> >       nio;
    MyStreambuf<char, std::char_traits<char> > nsb;
    nio.rdbuf (&nsb);

#ifndef _RWSTD_NO_WCHAR_T
    MyIos<wchar_t, std::char_traits<wchar_t> >       wio;
    MyStreambuf<wchar_t, std::char_traits<wchar_t> > wsb;
    wio.rdbuf (&wsb);
#endif // _RWSTD_NO_WCHAR_T

    // find all installed locales for which setlocale (LC_ALL) succeeds
    const char* const locale_list =
        rw_opt_locales ? rw_opt_locales : rw_locales (_RWSTD_LC_ALL);

    const std::size_t maxinx = RW_COUNT_OF (locales);

    for (const char* name = locale_list;
         *name;
         name += std::strlen (name) + 1) {

        const std::size_t inx = nlocales;
        locales [inx] = name;

        // fill in the money and results for this locale
        MyMoneyData& data = my_money_data [inx];
        data.locale_name_ = name;

        try {
            const std::locale loc (data.locale_name_);

            // initialize with random but valid values

            data.money_value_ = inx;
            data.type_ = MyMoneyData::PutId (nlocales % MyMoneyData::put_max);
            data.money_index_ = inx % RW_COUNT_OF (n_money_vals);

            // exercise domestic formats every other iteration
            // and international formats the rest
            data.intl_ = 0 == (inx & 1);

            // exercise postive and negative values
            if (inx & 1)
                data.money_value_ *= -1.;

            // add some random fractional digits
            if (inx & 2)
                data.money_value_ += data.money_value_ / 3.14;

            const std::money_put<char> &np =
                std::use_facet<std::money_put<char> >(loc);

            nio.imbue (loc);
            nsb.pubsetp (data.ncs_, RW_COUNT_OF (data.ncs_));
            
            switch (data.type_) {
            case MyMoneyData::put_ldbl:
                *np.put (std::ostreambuf_iterator<char>(&nsb),
                         data.intl_, nio, ' ', data.money_value_) = '\0';
                break;
            case MyMoneyData::put_string:
                *np.put (std::ostreambuf_iterator<char>(&nsb),
                         data.intl_, nio, ' ',
                         n_money_vals [data.money_index_]) = '\0';
                break;
            case MyMoneyData::put_max:
                // avoid enumeration value `put_max' not handled in switch
                // this case should never happen
                break;
            }

            rw_assert (!nio.fail (), __FILE__, __LINE__,
                       "money_put<char>::put(...) "
                       "failed for locale(%#s)",
                       data.locale_name_);

#ifndef _RWSTD_NO_WCHAR_T

            const std::money_put<wchar_t> &wp =
                std::use_facet<std::money_put<wchar_t> >(loc);

            wio.imbue (loc);
            wsb.pubsetp (data.wcs_, RW_COUNT_OF (data.wcs_));

            switch (data.type_) {
            case MyMoneyData::put_ldbl:
                *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
                         data.intl_, wio, L' ', data.money_value_) = '\0';
                break;
            case MyMoneyData::put_string:
                *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
                         data.intl_, wio, L' ',
                         w_money_vals [data.money_index_]) = L'\0';
                break;
            case MyMoneyData::put_max:
                // avoid enumeration value `put_max' not handled in switch
                // this case should never happen
                break;
            }

            rw_assert (!nio.fail (), __FILE__, __LINE__,
                       "money_put<wchar_t>::put(...) "
                       "failed for locale(%#s)",
                       data.locale_name_);

#endif // _RWSTD_NO_WCHAR_T

            if (opt_shared_locale)
                data.locale_ = loc;

            nlocales += 1;

        }
        catch (...) {
            rw_warn (!rw_opt_locales, 0, __LINE__,
                     "failed to create locale(%#s)", name);
        }

        if (nlocales == maxinx || nlocales == std::size_t (opt_nlocales))
            break;
    }

    // avoid divide by zero in thread if there are no locales to test
    rw_fatal (nlocales != 0, 0, __LINE__,
              "failed to create one or more usable locales!");

    rw_info (0, 0, 0,
             "testing std::money_put<charT> with %d thread%{?}s%{;}, "
             "%d iteration%{?}s%{;} each, in %zu locales { %{ .*A@} }",
             opt_nthreads, 1 != opt_nthreads,
             opt_nloops, 1 != opt_nloops,
             nlocales, int (nlocales), "%#s", locales);

    rw_info (0, 0, 0, "exercising std::money_put<char>");

    test_char  = true;
    test_wchar = false;

    // create and start a pool of threads and wait for them to finish
    int result = 
        rw_thread_pool (0, std::size_t (opt_nthreads), 0,
                        thread_func, 0, std::size_t (opt_timeout));

    rw_error (result == 0, 0, __LINE__,
              "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
              opt_nthreads, thread_func);

#ifndef _RWSTD_NO_WCHAR_T

    rw_info (0, 0, 0, "exercising std::money_put<wchar_t>");

    test_char  = false;
    test_wchar = true;

    // start a pool of threads to exercise wstring thread safety
    result =
        rw_thread_pool (0, std::size_t (opt_nthreads), 0,
                        thread_func, 0, std::size_t (opt_timeout));

    rw_error (result == 0, 0, __LINE__,
              "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
              opt_nthreads, thread_func);

    // exercise both the char and the wchar_t specializations
    // at the same time

    rw_info (0, 0, 0,
             "exercising both std::money_put<char> "
             "and std::money_put<wchar_t>");

    test_char  = true;
    test_wchar = true;

    // start a pool of threads to exercise wstring thread safety
    result =
        rw_thread_pool (0, std::size_t (opt_nthreads), 0,
                        thread_func, 0, std::size_t (opt_timeout));

    rw_error (result == 0, 0, __LINE__,
              "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
              opt_nthreads, thread_func);

#endif   // _RWSTD_NO_WCHAR_T

    return result;
}
Exemplo n.º 2
0
static int
run_test (int, char**)
{
    MyIos<char, std::char_traits<char> >       nio;
    MyStreambuf<char, std::char_traits<char> > nsb;
    nio.rdbuf (&nsb);

#ifndef _RWSTD_NO_WCHAR_T
    MyIos<wchar_t, std::char_traits<wchar_t> >       wio;
    MyStreambuf<wchar_t, std::char_traits<wchar_t> > wsb;
    wio.rdbuf (&wsb);
#endif // _RWSTD_NO_WCHAR_T

    // find all installed locales for which setlocale(LC_ALL) succeeds
    const char* const locale_list =
        rw_opt_locales ? rw_opt_locales : rw_locales (_RWSTD_LC_ALL);

    const std::size_t maxinx = RW_COUNT_OF (locales);

    for (const char *name = locale_list;
         *name;
         name += std::strlen (name) + 1) {

        const std::size_t inx = nlocales;
        locales [inx] = name;

        // fill in the value and results for this locale
        MyNumData& data = my_num_data [nlocales];
        data.locale_name_ = name;

        try {
            const std::locale loc (data.locale_name_);

            data.value_ = nlocales & 1 ? -1 * nlocales : nlocales;
            data.type_ = MyNumData::PutId (nlocales % MyNumData::put_max);

            // format data into buffers
            const std::num_put<char> &np =
                std::use_facet<std::num_put<char> >(loc);

            nio.imbue (loc);
            nsb.pubsetp (data.ncs_, RW_COUNT_OF (data.ncs_));

            put_data (data, np, std::ostreambuf_iterator<char>(&nsb),
                      nio, ' ', '\0');

            rw_fatal (!nio.fail (), __FILE__, __LINE__,
                      "num_put<char>::put(...) failed for locale(%#s)",
                      data.locale_name_);

#ifndef _RWSTD_NO_WCHAR_T

            const std::num_put<wchar_t> &wp =
                std::use_facet<std::num_put<wchar_t> >(loc);

            wio.imbue (loc);
            wsb.pubsetp (data.wcs_, RW_COUNT_OF (data.wcs_));

            put_data (data, wp, std::ostreambuf_iterator<wchar_t>(&wsb),
                      wio, L' ', L'\0');

            rw_fatal (!wio.fail (), __FILE__, __LINE__,
                      "num_put<wchar_t>::put(...) failed for locale(%#s)",
                      data.locale_name_);

#endif // _RWSTD_NO_WCHAR_T

            if (opt_shared_locale)
                data.locale_ = loc;

            nlocales += 1;
        }
        catch (...) {
            rw_warn (!rw_opt_locales, 0, __LINE__,
                     "failed to create locale(%#s)", name);
        }

        if (nlocales == maxinx || nlocales == std::size_t (opt_nlocales))
            break;
    }

    // avoid divide by zero in thread if there are no locales to test
    rw_fatal (nlocales != 0, 0, __LINE__,
              "failed to create one or more usable locales!");

    rw_info (0, 0, 0,
             "testing std::num_put<charT> with %d thread%{?}s%{;}, "
             "%d iteration%{?}s%{;} each, in %zu locales { %{ .*A@} }",
             opt_nthreads, 1 != opt_nthreads,
             opt_nloops, 1 != opt_nloops,
             nlocales, int (nlocales), "%#s", locales);

    rw_info (0, 0, 0, "exercising std::num_put<char>");

    test_char  = true;
    test_wchar = false;

    // create and start a pool of threads and wait for them to finish
    int result =
        rw_thread_pool (0, std::size_t (opt_nthreads), 0, thread_func, 0);

    rw_error (result == 0, 0, __LINE__,
              "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
              opt_nthreads, thread_func);

#ifndef _RWSTD_NO_WCHAR_T

    rw_info (0, 0, 0, "exercising std::num_put<wchar_t>");

    test_char  = false;
    test_wchar = true;

    // start a pool of threads to exercise wstring thread safety
    result =
        rw_thread_pool (0, std::size_t (opt_nthreads), 0, thread_func, 0);

    rw_error (result == 0, 0, __LINE__,
              "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
              opt_nthreads, thread_func);

    // exercise both the char and the wchar_t specializations
    // at the same time

    rw_info (0, 0, 0,
             "exercising both std::num_put<char> and std::num_put<wchar_t>");

    test_char  = true;
    test_wchar = true;

    // start a pool of threads to exercise wstring thread safety
    result =
        rw_thread_pool (0, std::size_t (opt_nthreads), 0, thread_func, 0);

    rw_error (result == 0, 0, __LINE__,
              "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
              opt_nthreads, thread_func);

#endif   // _RWSTD_NO_WCHAR_T

    return result;
}
Exemplo n.º 3
0
static void*
thread_func (void*)
{
    char             ncs [MyMoneyData::BufferSize];
    MyIos<char, std::char_traits<char> >       nio;
    MyStreambuf<char, std::char_traits<char> > nsb;
    nio.rdbuf (&nsb);

#ifndef _RWSTD_NO_WCHAR_T
    wchar_t                wcs [MyMoneyData::BufferSize];
    MyIos<wchar_t, std::char_traits<wchar_t> >       wio;
    MyStreambuf<wchar_t, std::char_traits<wchar_t> > wsb;
    wio.rdbuf (&wsb);
#endif // _RWSTD_NO_WCHAR_T

    for (int i = 0; i != opt_nloops; ++i) {

        if (rw_thread_pool_timeout_expired ())
            break;

        // save the name of the locale
        const MyMoneyData& data = my_money_data [i % nlocales];

        // construct a named locale, get a reference to the money_put
        // facet from it and use it to format a random money value
        const std::locale loc =
            opt_shared_locale ? data.locale_
                              : std::locale (data.locale_name_);

        if (test_char) {
            // exercise the narrow char specialization of the facet

            const std::money_put<char> &np =
                std::use_facet<std::money_put<char> >(loc);

            nio.imbue (loc);
            nsb.pubsetp (ncs, RW_COUNT_OF (ncs));

            switch (data.type_) {
            case MyMoneyData::put_ldbl:
                *np.put (std::ostreambuf_iterator<char>(&nsb),
                         data.intl_, nio, ' ', data.money_value_) = '\0';
                break;
            case MyMoneyData::put_string:
                *np.put (std::ostreambuf_iterator<char>(&nsb),
                         data.intl_, nio, ' ',
                         n_money_vals [data.money_index_]) = '\0';
                break;
            case MyMoneyData::put_max:
                // avoid enumeration value `put_max' not handled in switch
                // this case should never happen
                break;
            }

            RW_ASSERT (!nio.fail ());
            RW_ASSERT (!rw_strncmp (ncs, data.ncs_));

        }

        // both specializations may be tested at the same time

        if (test_wchar) {
            // exercise the wide char specialization of the facet

#ifndef _RWSTD_NO_WCHAR_T

            const std::money_put<wchar_t> &wp =
                std::use_facet<std::money_put<wchar_t> >(loc);

            wio.imbue (loc);
            wsb.pubsetp (wcs, RW_COUNT_OF (wcs));

            switch (data.type_) {
            case MyMoneyData::put_ldbl:
                *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
                         data.intl_, wio, ' ', data.money_value_) = L'\0';
                break;
            case MyMoneyData::put_string:
                *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
                         data.intl_, wio, ' ',
                         w_money_vals [data.money_index_]) = L'\0';
                break;
            case MyMoneyData::put_max:
                // avoid enumeration value `put_max' not handled in switch
                // this case should never happen
                break;
            }

            RW_ASSERT (!wio.fail ());
            RW_ASSERT (!rw_strncmp (wcs, data.wcs_));

#endif   // _RWSTD_NO_WCHAR_T

        }
    }

    return 0;
}
Exemplo n.º 4
0
static void*
thread_func (void*)
{
    char               ncs [MyNumData::BufferSize];
    MyIos<char, std::char_traits<char> >       nio;
    MyStreambuf<char, std::char_traits<char> > nsb;
    nio.rdbuf (&nsb);

#ifndef _RWSTD_NO_WCHAR_T
    wchar_t                  wcs [MyNumData::BufferSize];
    MyIos<wchar_t, std::char_traits<wchar_t> >       wio;
    MyStreambuf<wchar_t, std::char_traits<wchar_t> > wsb;
    wio.rdbuf (&wsb);
#endif // _RWSTD_NO_WCHAR_T

    for (int i = 0; i != opt_nloops; ++i) {

        // fill in the value and results for this locale
        const MyNumData& data = my_num_data [i % nlocales];

        // construct a named locale and imbue it in the ios object
        // so that the locale is used not only by the num_put facet
        const std::locale loc =
            opt_shared_locale ? data.locale_
                                 : std::locale (data.locale_name_);

        if (test_char) {
            // exercise the narrow char specialization of the facet

            const std::num_put<char> &np =
                std::use_facet<std::num_put<char> >(loc);

            nio.imbue (loc);
            nsb.pubsetp (ncs, RW_COUNT_OF (ncs));

            put_data (data, np, std::ostreambuf_iterator<char>(&nsb),
                      nio, ' ', '\0');

            RW_ASSERT (!nio.fail ()); 
            RW_ASSERT (!rw_strncmp (ncs, data.ncs_));
        }

        // both specializations may be tested at the same time

        if (test_wchar) {
            // exercise the wide char specialization of the facet

#ifndef _RWSTD_NO_WCHAR_T

            const std::num_put<wchar_t> &wp =
                std::use_facet<std::num_put<wchar_t> >(loc);

            wio.imbue (loc);
            wsb.pubsetp (wcs, RW_COUNT_OF (wcs));

            put_data (data, wp, std::ostreambuf_iterator<wchar_t>(&wsb),
                      wio, L' ', L'\0');

            RW_ASSERT (!wio.fail ()); 
            RW_ASSERT (!rw_strncmp (wcs, data.wcs_));

#endif   // _RWSTD_NO_WCHAR_T

        }
    }

    return 0;
}