static void test_1 () { UserClass *x = UserClass::from_char ("abcdef"); UserClass *y = UserClass::from_char ("ABCDEF"); const Iterator end = make_iter (x + 6, x + 0, x + 6, end); Iterator it = make_iter (x + 0, x + 0, x + 6, it); bool equal; // non-end iterator must compare unequal to the end iterator PASS (equal = it == end); rw_assert (!equal, 0, __LINE__, "InputIter<UserClass> unexpectedly equal to end: " "%p == %p", it.cur_, end.cur_); PASS (y [0] = *it); PASS (++it); PASS (equal = it == end); rw_assert (!equal, 0, __LINE__, "InputIter<UserClass> unexpectedly equal to end: " "%p == %p", it.cur_, end.cur_); PASS (y [1] = *it); PASS (++it); PASS (equal = it == it); rw_assert (equal, 0, __LINE__, "InputIter<UserClass> unexpectedly equal to self: " "%p == %p", it.cur_, it.cur_); PASS (y [2] = *it); PASS (++it); PASS (y [3] = *it); PASS (++it); PASS (y [4] = *it); PASS (++it); PASS (y [5] = *it); PASS (++it); PASS (equal = it == end); rw_assert (equal, 0, __LINE__, "InputIter<UserClass> unexpectedly not qual to end: " "%p != %p (diff = %d)", it.cur_, end.cur_, end.cur_ - it.cur_); rw_assert (0 == UserClass::compare (x, y, 6), 0, __LINE__, "InputIter<UserClass> data mismatch: %s != %s", X2STR (x, 6), X2STR (y, 6)); delete[] x; delete[] y; }
static void test_0 () { UserClass *x = UserClass::from_char ("abc"); Iterator end0 = make_iter (x + 3, x + 0, x + 3, end0); Iterator end1 = make_iter (x + 3, x + 0, x + 3, end1); bool equal; // end iterator must compare equal to itself PASS (equal = end0 == end0); rw_assert (equal, 0, __LINE__, "InputIter<UserClass> end iterator unexpectedly " "not equal to self: %p == %p", end0.cur_, end0.cur_); PASS (equal = end0 != end0); rw_assert (!equal, 0, __LINE__, "InputIter<UserClass> end iterator unexpectedly " "not equal to self: %p == %p", end0.cur_, end0.cur_); // end iterator must compare equal to another PASS (equal = end0 == end1); rw_assert (equal, 0, __LINE__, "InputIter<UserClass> end iterator unexpectedly " "not equal to another: %p == %p", end0.cur_, end1.cur_); PASS (equal = end0 != end1); rw_assert (!equal, 0, __LINE__, "InputIter<UserClass> end iterator unexpectedly " "not equal to another: %p == %p", end0.cur_, end1.cur_); // cannot increment the end iterator FAIL (++end0); FAIL (end0++); FAIL (++end1); FAIL (end1++); // cannot dereference the end iterator FAIL (x [0] = *end0); FAIL (x [0] = *end1); FAIL (equal = int ('a') == end0->data_.val_); FAIL (equal = int ('a') == end1->data_.val_); delete[] x; }
kmapdset_iterator_impl_base* kmapdset_base::upper_bound_impl(const void* k1, DB_TXN* txn, const char* func) { PortableDataOutput<AutoGrownMemIO> oKey1; save_key1(oKey1, k1); DBT tk1; memset(&tk1, 0, sizeof(DBT)); tk1.data = oKey1.begin(); tk1.size = oKey1.tell(); kmapdset_iterator_impl_base* iter = make_iter(); iter->init(m_db, txn, func); iter->m_ret = iter->m_curp->get(iter->m_curp, &tk1, &iter->m_bulk, DB_SET_RANGE|DB_MULTIPLE); if (0 == iter->m_ret) { DBT kbak; memset(&kbak, 0, sizeof(DBT)); kbak.data = oKey1.begin(); kbak.size = oKey1.tell(); int cmp = m_bt_comp(m_db, &kbak, &tk1); assert(cmp <= 0); if (0 == cmp) { iter->m_ret = iter->m_curp->get(iter->m_curp, &tk1, &iter->m_bulk, DB_NEXT_NODUP|DB_MULTIPLE); if (0 == iter->m_ret) iter->bulk_load(&tk1); } else iter->bulk_load(&tk1); } if (0 != iter->m_ret && DB_NOTFOUND != iter->m_ret) { string_appender<> oss; oss << db_strerror(iter->m_ret) << "... at: " << func << "\n" ; throw std::runtime_error(oss.str()); } return iter; }
int main(int argc, char** argv) { if(argc < 3) { printf("Usage: ./gol file.in iters [--check]\n"); exit(1); } int iters = atoi(argv[2]); FILE* fin = fopen(argv[1], "r"); if(fin == NULL) { printf("Cannot open file %s, are you sure it exists ?\n", argv[1]); exit(2); } int matsz; int rd = fscanf(fin, "%d\n", &matsz); //printf("Matrix size = %d\n", matsz); int i, j; char** matrix = calloc(matsz, sizeof(char*)); char* lndata = calloc(matsz + 1, 1); for(i = 0; i < matsz; i++) { char* ln = calloc(matsz, 1); int ret = fread(lndata, matsz + 1, 1, fin); for(j = 0; j < matsz; j++) { ln[j] = lndata[j] - '0'; } matrix[i] = ln; } free(lndata); char** nmat = calloc(matsz, sizeof(char*)); for(i = 0; i < matsz; i++) { char* ln = calloc(matsz, 1); memcpy(ln, matrix[i], matsz); nmat[i] = ln; } fclose(fin); char** matrs[2] = {matrix, nmat}; int idx[2] = {0, 1}; for(i = 0; i < iters; i++) { make_iter(matrs[idx[0]], matrs[idx[1]], matsz); idx[0] ^= 1; idx[1] ^= 1; } if(argc == 4) { char** finmat = matrs[iters % 2]; for(i = 0; i < matsz; i++) { for(j = 0; j < matsz; j++) { printf("%d", finmat[i][j]); } printf("\n"); } } for(i = 0; i < matsz; i++) { free(matrix[i]); free(nmat[i]); } free(matrix); free(nmat); }
kmapdset_iterator_impl_base* kmapdset_base::find_impl(const void* k1, const void* k2, DB_TXN* txn, bool bulk, const char* func) { kmapdset_iterator_impl_base* iter = make_iter(); iter->init(m_db, txn, func); try { bool bRet = iter->find_pos(k1, k2, bulk, func); (void)bRet; return iter; } catch (std::exception& exp) { delete iter; iter = 0; throw exp; } }
kmapdset_iterator_impl_base* kmapdset_base::begin_impl(DB_TXN* txn, const char* func) { kmapdset_iterator_impl_base* iter = make_iter(); iter->init(m_db, txn, func); DBT tk1; memset(&tk1, 0, sizeof(DBT)); iter->m_ret = iter->m_curp->get(iter->m_curp, &tk1, &iter->m_bulk, DB_FIRST|DB_MULTIPLE); if (0 == iter->m_ret) { iter->bulk_load(&tk1); } else if (DB_NOTFOUND != iter->m_ret) { string_appender<> oss; oss << db_strerror(iter->m_ret) << "... at: " << func; delete iter; iter = 0; throw std::runtime_error(oss.str()); } return iter; }
kmapdset_iterator_impl_base* kmapdset_base::find_impl(const void* k1, DB_TXN* txn, u_int32_t flags, const char* func) { PortableDataOutput<AutoGrownMemIO> oKey1; save_key1(oKey1, k1); DBT tk1; memset(&tk1, 0, sizeof(DBT)); tk1.data = oKey1.begin(); tk1.size = oKey1.tell(); kmapdset_iterator_impl_base* iter = make_iter(); iter->init(m_db, txn, func); iter->m_ret = iter->m_curp->get(iter->m_curp, &tk1, &iter->m_bulk, flags); if (0 == iter->m_ret) { iter->bulk_load(&tk1); } else if (DB_NOTFOUND != iter->m_ret && DB_KEYEMPTY != iter->m_ret) { string_appender<> oss; oss << db_strerror(iter->m_ret) << "... at: " << func << "\n" << "flags=" << flags ; throw std::runtime_error(oss.str()); } return iter; }
static void test_3 () { UserClass *x = UserClass::from_char ("abcdef"); UserClass *y = UserClass::from_char ("ABCDEF"); const Iterator end = make_iter (x + 6, x + 6, x + 6, end); Iterator it0 = make_iter (x + 0, x + 0, x + 6, it0); Iterator it1 = it0; bool equal; PASS (equal = it0 == it1); rw_assert (equal, 0, __LINE__, "InputIter<UserClass> unexpectedly not equal: " "%p != %p (diff = %d)", it0.cur_, it1.cur_, it0.cur_ - it1.cur_); PASS (equal = it0 == end); rw_assert (!equal, 0, __LINE__, "InputIter<UserClass> unexpectedly equal to end: " "%p == %p", it1.cur_, end.cur_); PASS (y [0] = *it0); PASS (y [1] = *it1); rw_assert (y [0].data_.val_ == y [1].data_.val_, 0, __LINE__, "two copies of InputIter<UserClass> unexpectedly yield " "different values: %d != %d", y [0].data_.val_, y [1].data_.val_); PASS (it0++); FAIL (it1++); // can't pass through the same iterator twice FAIL (it0 == it1); // it1 not in the domain of operator==() PASS (equal = it0 == end); rw_assert (!equal, 0, __LINE__, "InputIter<UserClass> unexpectedly equal to end: " "%p == %p", it1.cur_, end.cur_); PASS (it1 = it0); PASS (equal = it0 == it1); rw_assert (equal, 0, __LINE__, "InputIter<UserClass> unexpectedly not equal: " "%p == %p", it0.cur_, it1.cur_); PASS (y [0] = *it0); PASS (y [1] = *it1); rw_assert (y [0].data_.val_ == y [1].data_.val_, 0, __LINE__, "two copies of InputIter<UserClass> unexpectedly yield " "different values: %d != %d", y [0].data_.val_, y [1].data_.val_); rw_assert (y [0].data_.val_ == 'b', 0, __LINE__, "InputIter<UserClass>::operator*() == %d, got %d", y [0].data_.val_, 'b'); PASS (it1++); FAIL (it0++); // can't pass through the same iterator twice FAIL (it0 == it1); // it0 not in the domain of operator==() PASS (equal = it1 == end); rw_assert (!equal, 0, __LINE__, "InputIter<UserClass> unexpectedly equal to end: " "%p == %p", it1.cur_, end.cur_); FAIL (x [0] = *it0); // cannot dereference delete[] x; delete[] y; }
void do_test (int line, // line number of test case const char *src, // source sequence std::size_t resoff, // offset of result ForwardIterator dummy_iter, const T*, const char* predname) { static const char* const itname = type_name (dummy_iter, (T*)0); const std::size_t nsrc = std::strlen (src); if (std::size_t (-1) == resoff) resoff = nsrc; // have the UserClass default ctor initialize objects from `src' xinit_begin = src; UserClass::gen_ = xinit; UserClass* const xsrc = new UserClass [nsrc]; const ForwardIterator first = make_iter (xsrc, xsrc, xsrc + nsrc, dummy_iter); const ForwardIterator last = make_iter (xsrc + nsrc, xsrc, xsrc + nsrc, dummy_iter); // reset predicate counters UserClass::n_total_op_eq_ = 0; EqualityPredicate<T, T>::funcalls_ = 0; // construct a predicate object const EqualityPredicate<T, T> pred (0, 0); const ForwardIterator res = predname ? std::adjacent_find (first, last, pred) : std::adjacent_find (first, last); // silence a bogus EDG eccp remark #550-D: // variable "res" was set but never used _RWSTD_UNUSED (res); const std::size_t n_total_pred = predname ? EqualityPredicate<T, T>::funcalls_ : UserClass::n_total_op_eq_; // verify that the returned iterator is set as expected int success = res.cur_ == first.cur_ + resoff; rw_assert (success, 0, line, "line %d: adjacent_find<%s>(it = \"%s\", ...)" " == (it + %zu), got (it + %td)", __LINE__, itname, src, resoff, res.cur_ - first.cur_); // verify the number of applications of the predicate (lwg issue 240): // Complexity: For a nonempty range, exactly // min((i - first) + 1, (last - first) - 1) // applications of the corresponding predicate, where i is // adjacent_find's return value. // compute the expected number of invocations of the predicate std::size_t n_expect_pred = 0; if (nsrc) { // test iterators are guaranteed to be in range _RWSTD_ASSERT (first.cur_ <= res.cur_ && res.cur_ <= last.cur_); n_expect_pred = std::size_t (res.cur_ - first.cur_) + 1; const std::size_t tmp = std::size_t (last.cur_ - first.cur_) - 1; if (tmp < n_expect_pred) n_expect_pred = tmp; } success = std::size_t (n_expect_pred) == n_total_pred; rw_assert (success, 0, line, "line %d: adjacent_find<%s>(\"%s\", ...) " "invoked %s %zu times, expected %td", __LINE__, itname, src, predname ? predname : "operator==()", n_total_pred, n_expect_pred); }
void do_test (int line, // line number of test case const char *src, // source sequence std::size_t resoff, // offset of result ForwardIterator dummy_iter, const T*, const char* predicate) { static const char* const itname = type_name (dummy_iter, (T*)0); #ifdef __SYMBIAN32__ const size_t nsrc = strlen (src); #else const std::size_t nsrc = std::strlen (src); #endif //__SYMBIAN32__ if (std::size_t (-1) == resoff) resoff = nsrc; // have the X default ctor initialize objects from `src' xinit_begin = src; #ifndef __SYMBIAN32__ X::gen_ = xinit; #else gen_ = xinit; #endif X* const xsrc = new X [nsrc]; const ForwardIterator first = make_iter (xsrc, xsrc, xsrc + nsrc, dummy_iter); const ForwardIterator last = make_iter (xsrc + nsrc, xsrc, xsrc + nsrc, dummy_iter); // compute the number of invocations of the predicate #ifndef __SYMBIAN32__ std::size_t n_total_pred = X::n_total_op_eq_; #else std::size_t n_total_pred = n_total_op_eq_; #endif const ForwardIterator res = predicate ? std::adjacent_find (first, last, std::equal_to<X>()) : std::adjacent_find (first, last); // silence a bogus EDG eccp remark #550-D: // variable "res" was set but never used _RWSTD_UNUSED (res); #ifndef __SYMBIAN32__ n_total_pred =X::n_total_op_eq_ - n_total_pred; #else n_total_pred = n_total_op_eq_ - n_total_pred; #endif // verify that the returned iterator is set as expected int success = res.cur_ == first.cur_ + resoff; if(!success) { failures++; std_log(LOG_FILENAME_LINE,"Reason: Failing as expected iterator is not returned"); } rw_assert (success, 0, line, "line %d: adjacent_find<%s>(it = \"%s\", ...)" " == (it + %zu), got (it + %td)", __LINE__, itname, src, resoff, res.cur_ - first.cur_); // verify the number of applications of the predicate (lwg issue 240): // Complexity: For a nonempty range, exactly // min((i - first) + 1, (last - first) - 1) // applications of the corresponding predicate, where i is // adjacent_find's return value. // compute the expected number of invocations of the predicate std::size_t n_expect_pred = 0; if (nsrc) { // test iterators are guaranteed to be in range _RWSTD_ASSERT (first.cur_ <= res.cur_ && res.cur_ <= last.cur_); n_expect_pred = std::size_t (res.cur_ - first.cur_) + 1; const std::size_t tmp = std::size_t (last.cur_ - first.cur_) - 1; if (tmp < n_expect_pred) n_expect_pred = tmp; } success = std::size_t (n_expect_pred) == n_total_pred; if(!success) { failures ++; std_log(LOG_FILENAME_LINE,"Reason: Failing as iteratoris not in range"); } rw_assert (success, 0, line, "line %d: adjacent_find<%s>(\"%s\", ...) " "invoked %s %zu times, expected %td", __LINE__, itname, src, predicate ? predicate : "operator==()", n_total_pred, n_expect_pred); }
void test_fill (std::size_t N, const ForwardIterator& fill_iter, const T*) { static const char* const itname = type_name (fill_iter, (T*) 0); static const char* const tname = "X"; rw_info (0, 0, 0, "void std::fill (%s, %1$s, const %s&)", itname, tname); // generate sequential values for each default constructed T //T::gen_ = gen_seq; gen_ = gen_seq; // use ::operator new() to prevent default initialization T *buf = _RWSTD_STATIC_CAST (T*, ::operator new (N * sizeof (T))); // default-construct the first T at buf[0] new (buf) T (); for (std::size_t i = 0; i < N; ++i) { // default-construct a new X at the end of buf T* const new_t = new (buf + i) T (); // exercise 25.2.5 - std::fill<> () // std::size_t last_n_op_assign = T::n_total_op_assign_; std::size_t last_n_op_assign = n_total_op_assign_; T* const buf_end = buf + i + 1; const ForwardIterator begin = make_iter (buf, buf, buf_end, fill_iter); const ForwardIterator end = make_iter (buf_end, buf_end, buf_end, fill_iter); std::fill (begin, end, *new_t); // verify 25.2.5, p2 bool success = true; std::size_t j = 0; for ( ; j != i + 1; ++j) { success = buf[j].val_ == new_t->val_; if (!success) break; } if(!success) { failures++; std_log(LOG_FILENAME_LINE,"Reason: Failing as %d element has not expected value",j); } rw_assert (success, 0, __LINE__, "%zu. fill (%s, %1$s, const %s&): buf[%zu]: %d != %d", i + 1, itname, tname, j, buf[j].val_, new_t->val_); if (!success) break; // verify 25.2.5, p3 // success = T::n_total_op_assign_ - last_n_op_assign == i + 1; success = n_total_op_assign_ - last_n_op_assign == i + 1; /* rw_assert (success, 0, __LINE__, "%zu. fill (%s, %1$s, const %s&) complexity: " "%zu != %zu", i + 1, itname, tname, T::n_total_op_assign_ - last_n_op_assign, i + 1); */ if(!success) { failures++; std_log(LOG_FILENAME_LINE,"Reason: Failing ",j); } rw_assert (success, 0, __LINE__, "%zu. fill (%s, %1$s, const %s&) complexity: " "%zu != %zu", i + 1, itname, tname, n_total_op_assign_ - last_n_op_assign, i + 1); if (!success) break; } ::operator delete (buf); }
kmapdset_iterator_impl_base* kmapdset_base::end_impl(DB_TXN* txn, const char* func) { kmapdset_iterator_impl_base* iter = make_iter(); iter->init(m_db, txn, func); return iter; }