From f8f85dcf9de45cbb42ee46b6f6e3d1e685b0103a Mon Sep 17 00:00:00 2001 From: Eric Blossom Date: Sat, 15 Aug 2009 01:02:15 -0700 Subject: Additional QA for tuple. --- gruel/src/lib/pmt/pmt.cc | 20 +++++++++++++++++--- gruel/src/lib/pmt/pmt_io.cc | 5 +++++ gruel/src/lib/pmt/qa_pmt_prims.cc | 19 ++++++++++++++++--- 3 files changed, 38 insertions(+), 6 deletions(-) (limited to 'gruel/src/lib/pmt') diff --git a/gruel/src/lib/pmt/pmt.cc b/gruel/src/lib/pmt/pmt.cc index 8bf5b2dea..20dc4e42c 100644 --- a/gruel/src/lib/pmt/pmt.cc +++ b/gruel/src/lib/pmt/pmt.cc @@ -673,20 +673,21 @@ pmt_to_tuple(const pmt_t &x) size_t len = pmt_length(x); pmt_tuple *t = new pmt_tuple(len); + pmt_t r = pmt_t(t); if (x->is_vector()){ for (size_t i = 0; i < len; i++) t->_set(i, _vector(x)->ref(i)); - return pmt_t(t); + return r; } if (x->is_pair()){ pmt_t y = x; for (size_t i = 0; i < len; i++){ t->_set(i, pmt_car(y)); - y = pmt_cdr(x); + y = pmt_cdr(y); } - return pmt_t(t); + return r; } throw pmt_wrong_type("pmt_to_tuple", x); @@ -928,6 +929,19 @@ pmt_equal(const pmt_t& x, const pmt_t& y) return true; } + if (x->is_tuple() && y->is_tuple()){ + pmt_tuple *xv = _tuple(x); + pmt_tuple *yv = _tuple(y); + if (xv->length() != yv->length()) + return false; + + for (unsigned i = 0; i < xv->length(); i++) + if (!pmt_equal(xv->_ref(i), yv->_ref(i))) + return false; + + return true; + } + if (x->is_uniform_vector() && y->is_uniform_vector()){ pmt_uniform_vector *xv = _uniform_vector(x); pmt_uniform_vector *yv = _uniform_vector(y); diff --git a/gruel/src/lib/pmt/pmt_io.cc b/gruel/src/lib/pmt/pmt_io.cc index f5a82de0e..fbc556ab0 100644 --- a/gruel/src/lib/pmt/pmt_io.cc +++ b/gruel/src/lib/pmt/pmt_io.cc @@ -90,6 +90,11 @@ pmt_write(pmt_t obj, std::ostream &port) // port << "#"; port << "#"; } + else if (pmt_is_tuple(obj)){ + // FIXME + // port << "#"; + port << "#"; + } else if (pmt_is_uniform_vector(obj)){ // FIXME // port << "#"; diff --git a/gruel/src/lib/pmt/qa_pmt_prims.cc b/gruel/src/lib/pmt/qa_pmt_prims.cc index d31e3fabf..cf1ee0142 100644 --- a/gruel/src/lib/pmt/qa_pmt_prims.cc +++ b/gruel/src/lib/pmt/qa_pmt_prims.cc @@ -207,14 +207,13 @@ check_tuple(size_t len, const std::vector &s, pmt_t t) void qa_pmt_prims::test_tuples() { - pmt_t t0 = pmt_make_tuple(); - CPPUNIT_ASSERT_EQUAL(size_t(0), pmt_length(t0)); - + pmt_t v = pmt_make_vector(10, PMT_NIL); std::vector s(10); for (size_t i = 0; i < 10; i++){ std::ostringstream os; os << "s" << i; s[i] = pmt_string_to_symbol(os.str()); + pmt_vector_set(v, i, s[i]); } pmt_t t; @@ -254,6 +253,20 @@ qa_pmt_prims::test_tuples() t = pmt_make_tuple(s[0], s[1], s[2]); CPPUNIT_ASSERT_THROW(pmt_tuple_ref(t, 3), pmt_out_of_range); + + t = pmt_make_tuple(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9]); + check_tuple(10, s, t); + + pmt_t t2 = pmt_to_tuple(v); + CPPUNIT_ASSERT_EQUAL(size_t(10), pmt_length(v)); + CPPUNIT_ASSERT(pmt_equal(t, t2)); + + t = pmt_make_tuple(s[0], s[1], s[2]); + pmt_t list0 = pmt_list3(s[0], s[1], s[2]); + CPPUNIT_ASSERT_EQUAL(size_t(3), pmt_length(list0)); + t2 = pmt_to_tuple(list0); + CPPUNIT_ASSERT_EQUAL(size_t(3), pmt_length(t2)); + CPPUNIT_ASSERT(pmt_equal(t, t2)); } void -- cgit