TEST(VersionTest, InitWithValue) { Int a = 3; Int b = a + 4; EXPECT_EQ(0, a.get_version()); EXPECT_EQ(0, b.get_version()); }
TEST(IfTest, ThenElseWithSingleVar) { bool ok; Int i = any_int("I"); If branch(i < 5); branch.track(i); ok = branch.begin_then(); EXPECT_TRUE(ok); i = i + 1; std::stringstream out_then; out_then << i.get_value().get_expr(); EXPECT_EQ("([I]+1)", out_then.str()); ok = branch.begin_else(); EXPECT_TRUE(ok); i = i + 2; std::stringstream out_else; out_else << i.get_value().get_expr(); EXPECT_EQ("([I]+2)", out_else.str()); branch.end(); std::stringstream out_end; out_end << i.get_value().get_expr(); EXPECT_EQ("(([I]<5)?([I]+1):([I]+2))", out_end.str()); }
void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::false_ const&, mpl::false_ const&) { // Load. // Non-trivial. // Non binary. bool s; ar & s; std::size_t limb_count; std::size_t byte_count; ar & byte_count; limb_count = byte_count / sizeof(limb_type) + (byte_count % sizeof(limb_type) ? 1 : 0); val.resize(limb_count, limb_count); limb_type* pl = val.limbs(); for(std::size_t i = 0; i < limb_count; ++i) { pl[i] = 0; for(std::size_t j = 0; (j < sizeof(limb_type)) && byte_count; ++j) { unsigned char byte; ar & byte; pl[i] |= static_cast<limb_type>(byte) << (j * CHAR_BIT); --byte_count; } } if(s != val.sign()) val.negate(); val.normalize(); }
// k is equal to the number of loop iterations induced by concrete values. TEST(LoopTest, UnwindWithConcreteConditionEqualK) { bool ok; Int i = 1; Loop loop(1u); loop.track(i); // 1x ok = loop.unwind(i < 7); EXPECT_TRUE(ok); i = i + 2; std::stringstream out_1x; out_1x << i.get_value().get_expr(); EXPECT_EQ("3", out_1x.str()); // 2x ok = loop.unwind(i < 3); EXPECT_FALSE(ok); // unwind(const Value<bool>&) call for 3x is undefined because ok == false. std::stringstream out; out << i.get_value().get_expr(); EXPECT_EQ("3", out.str()); }
TEST(IfTest, VersionAfterThenElseWithSingleAnyVar) { bool ok; Int i = any_int("I"); If branch(i < 5); branch.track(i); ok = branch.begin_then(); EXPECT_TRUE(ok); i = i + any_int("A"); EXPECT_EQ(VZERO + 1, i.get_version()); ok = branch.begin_else(); EXPECT_TRUE(ok); EXPECT_EQ(VZERO + 2, i.get_version()); i = i + any_int("A"); EXPECT_EQ(VZERO + 3, i.get_version()); branch.end(); // version increase due to join operation EXPECT_EQ(VZERO + 4, i.get_version()); }
// k is less than the number of loop iterations induced by concrete values. TEST(LoopTest, UnwindWithConcreteConditionLessThanK) { bool ok; Int i = 1; Loop loop(1u); loop.track(i); // 1x ok = loop.unwind(i < 7); EXPECT_TRUE(ok); i = i + 2; std::stringstream out_1x; out_1x << i.get_value().get_expr(); EXPECT_EQ("3", out_1x.str()); // 2x ok = loop.unwind(i < 7); EXPECT_TRUE(ok); i = i + 4; std::stringstream out_2x; out_2x << i.get_value().get_expr(); EXPECT_EQ("7", out_2x.str()); // 3x ok = loop.unwind(i < 7); EXPECT_FALSE(ok); std::stringstream out; out << i.get_value().get_expr(); EXPECT_EQ("7", out.str()); }
TEST(LoopTest, Unwind2xWithSingleVarWithAny) { bool ok; Int i = any_int("I"); Loop loop(2u); loop.track(i); // 1x ok = loop.unwind(i < 5); EXPECT_TRUE(ok); i = i + any_int("A"); std::stringstream out_1x; out_1x << i.get_value().get_expr(); EXPECT_EQ("([I]+[A])", out_1x.str()); // 2x ok = loop.unwind(i < 7); EXPECT_TRUE(ok); i = i + any_int("B"); std::stringstream out_2x; out_2x << i.get_value().get_expr(); EXPECT_EQ("(([I]+[A])+[B])", out_2x.str()); // 3x ok = loop.unwind(any_bool("ANY")); EXPECT_FALSE(ok); std::stringstream join_out; join_out << i.get_value().get_expr(); EXPECT_EQ("(([I]<5)?((([I]+[A])<7)?(([I]+[A])+[B]):([I]+[A])):[I])", join_out.str()); }
Variable* negate(Variable* A) { if(A == NULL) { interpreter.error("Error: Void variable in negation.\n"); return NULL; } TypeEnum a = A->getType(); if(a == STRING || a == BOOL || a == MACRO || a == ARRAY || a == LIST || a == FUNCTION || a == PROCEDURE) { interpreter.error("Error: Negation not defined for type '%s'\n", getTypeString(a).c_str()); return NULL; } if(a == INT) { Int* C = static_cast<Int*>(A); Int* R = new Int; R->setValue(-C->getValue()); return R; } else if(a == FLOAT) { Float* C = static_cast<Float*>(A); Float* R = new Float; R->setValue(-C->getValue()); return R; } return A; }
int main() { Int *a = new Int; Int *b = new Int; a->i = 1; b->i = 2; printf("%s %d\n", a->str(), a->nb_add(b)->i); }
Variable* array_access(Variable* A, Variable* B) { if(A == NULL || B == NULL) { interpreter.error("Error: Void variable in array_access operation.\n"); return NULL; } TypeEnum a = A->getType(); TypeEnum b = B->getType(); if(a != ARRAY || b != INT) { interpreter.error("Error: Array access not defined for types '%s' and '%s'\n", getTypeString(a).c_str(), getTypeString(b).c_str()); return NULL; } Array* C = static_cast<Array*>(A); Int* D = static_cast<Int*>(B); vector<Variable*>& v = C->getValue(); Variable* result = NULL; int index = D->getValue(); if(index >= 0 && (unsigned int)index < v.size()) result = v[index]; if(result == NULL) interpreter.error("Error: Array index out of bounds.\n"); return result; }
void IntConsumer::run() { Int * in = consume(input); log("consuming "+in->to_string()); timer.randSleep(200); release(input); }
std::string Num_to_Words(Int toconvert){ std::string toreturn(toconvert.sign() == -1 ? "negative " : ""); toconvert = toconvert.magnitude(); while(toconvert > 0){ size_t i( toconvert.count_digits() ); //key1 accesses words of order 10, like "thousand" //key2 accesses the single digits words, like "one" Int key1(Math::exponentiate(Int(k_ten), Int(i-1))), key2(toconvert / key1) ; switch(i){ case 1: //One digit toreturn += NumberWordBank.at(key2); break; case 2: //Two digits if(toconvert >= 2*k_ten) toreturn += NumberWordBank.at(key2 * k_ten) + (toconvert%k_ten > 0 ? "-" : "") ; else if(toconvert >= k_ten){ toreturn += NumberWordBank.at(toconvert); return toreturn; } break; case 3: //Three digits toreturn += NumberWordBank.at(key2) + ' '; toreturn += NumberWordBank.at(key1) + ' '; break; default: //Four or more digits key2 = toconvert; key1 = 1; while(key2.count_digits() > 3){ key1 *= k_thousand; key2 /= k_thousand; } toreturn += Num_to_Words(key2) + ' '; toreturn += NumberWordBank.at(key1) + ' '; break; } //Stop adding things once toconvert is 0 if(key1*key2 == toconvert) break; //Inserting "and" if( (i == 3 && toconvert%100 > 0) || (i >= 4 && toconvert%k_thousand < 100) ) toreturn += k_and; toconvert -= key1*key2; } while(toreturn.back() == ' ') toreturn.pop_back(); return toreturn; }
void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::true_ const&, mpl::true_ const&) { // Store. // Trivial. // Binary. bool s = val.sign(); ar & s; ar.save_binary(val.limbs(), sizeof(*val.limbs())); }
TEST(VersionTest, InitWithAny) { Bool a = any_bool("A"); Char b = any_char("B"); Int c = any_int("C"); EXPECT_EQ(0, a.get_version()); EXPECT_EQ(0, b.get_version()); EXPECT_EQ(0, c.get_version()); }
TEST(VersionTest, InitWithVarRequiringCast) { Bool a = any_bool("A"); Char b = any_char("B"); Int c = b; EXPECT_EQ(0, a.get_version()); EXPECT_EQ(0, b.get_version()); EXPECT_EQ(0, c.get_version()); }
void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::false_ const&, mpl::true_ const&) { // Store. // Non-trivial. // Binary. bool s = val.sign(); std::size_t c = val.size(); ar & s; ar & c; ar.save_binary(val.limbs(), c * sizeof(limb_type)); }
void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::true_ const&, mpl::true_ const&) { // Load. // Trivial. // Binary. bool s; ar & s; ar.load_binary(val.limbs(), sizeof(*val.limbs())); if(s != val.sign()) val.negate(); }
Text operator[](Int i) const { if (is_na() || (static_cast<size_t>(i.raw()) >= raw_size())) { return Text::na(); } if (is_direct_) { return data_[i.raw()]; } else { return Text(&bodies_[headers_[i.raw()].offset], headers_[i.raw()].size.raw()); } }
TEST(VersionTest, InitWithConcrete) { Bool a = true; Bool b = true; Char c = 'a'; Int d = 3; EXPECT_EQ(0, a.get_version()); EXPECT_EQ(0, b.get_version()); EXPECT_EQ(0, c.get_version()); EXPECT_EQ(0, d.get_version()); }
//----------------------------------------------------------------------------- int DataType::GetInt() const { HRESULT hr; Int i; hr = i.Cast( *this ); if( FAILED(hr) ) return 0; return i.Get(); }
TEST(VersionTest, SelfAssignmentAfterOperations) { Int a = 3; Int b = any_int("B"); Char c = 'c'; EXPECT_EQ(0, a.get_version()); EXPECT_EQ(0, b.get_version()); EXPECT_EQ(0, c.get_version()); // some operations a = a + b + c; b = a + b + c; b = 5; c = c + 'x'; c = 'y'; c = 'z' + c; EXPECT_EQ(1, a.get_version()); EXPECT_EQ(2, b.get_version()); EXPECT_EQ(3, c.get_version()); a = a; b = b; c = c; EXPECT_EQ(1, a.get_version()); EXPECT_EQ(2, b.get_version()); EXPECT_EQ(3, c.get_version()); }
void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::false_ const&, mpl::true_ const&) { // Load. // Non-trivial. // Binary. bool s; std::size_t c; ar & s; ar & c; val.resize(c, c); ar.load_binary(val.limbs(), c * sizeof(limb_type)); if(s != val.sign()) val.negate(); val.normalize(); }
TEST(settings_types, Int) { Int i; ASSERT_TRUE(i.set_default("1")); ASSERT_STREQ("1", i.get_default().c_str()); ASSERT_STREQ("1", i.get_value().c_str()); ASSERT_TRUE(i.is_default()); ASSERT_TRUE(i.set_value("9")); ASSERT_FALSE(i.is_default()); ASSERT_EQ(9, i.value()); ASSERT_EQ(1, i.default_value()); }
Variable* exponentiate(Variable* A, Variable* B) { if(A == NULL || B == NULL) { interpreter.error("Error: Void variable in exponentiation.\n"); return NULL; } TypeEnum a = A->getType(); TypeEnum b = B->getType(); if(a == STRING || a == BOOL || a == MACRO || a == ARRAY || a == LIST || a == FUNCTION || a == PROCEDURE || b == STRING || b == BOOL || b == MACRO || b == ARRAY || b == LIST || b == FUNCTION || b == PROCEDURE) { interpreter.error("Error: Exponentiation not defined for types '%s' and '%s'\n", getTypeString(a).c_str(), getTypeString(b).c_str()); return NULL; } if(a == INT) { Int* C = static_cast<Int*>(A); if(b == INT) { Int* D = static_cast<Int*>(B); Int* R = new Int; R->setValue(pow(float(C->getValue()), D->getValue())); return R; } else if(b == FLOAT) { Float* D = static_cast<Float*>(B); Float* R = new Float; R->setValue(pow(C->getValue(), D->getValue())); return R; } } else if(a == FLOAT) { Float* C = static_cast<Float*>(A); if(b == INT) { Int* D = static_cast<Int*>(B); Float* R = new Float; R->setValue(pow(C->getValue(), D->getValue())); return R; } else if(b == FLOAT) { Float* D = static_cast<Float*>(B); Float* R = new Float; R->setValue(pow(C->getValue(), D->getValue())); return R; } } return A; }
// Return a value. // // If "row_id" is valid, returns the stored value. // If "row_id" is invalid, returns N/A. GeoPoint get(Int row_id) const { size_t value_id = row_id.raw(); if (value_id >= values_.size()) { return GeoPoint::na(); } return values_[value_id]; }
void SketchBoard::run() { Int * in = consume(input); Float3D * out = produce(output); int k = *in->get(); out->get()->x = sin(k); out->get()->y = cos(k); out->get()->z = 1/10.0; log("producing "+to_string(stepno)); release(output); release(input); }
void Column<Float>::set(Int row_id, const Datum &datum) { Float new_value = parse_datum(datum); if (!table_->test_row(row_id)) { throw "Invalid row ID"; // TODO } if (new_value.is_na()) { unset(row_id); return; } Float old_value = get(row_id); if (old_value.match(new_value)) { return; } if (!old_value.is_na()) { // Remove the old value from indexes. for (size_t i = 0; i < num_indexes(); ++i) { indexes_[i]->remove(row_id, old_value); } } size_t value_id = row_id.raw(); if (value_id >= values_.size()) { values_.resize(value_id + 1, Float::na()); } // Insert the new value into indexes. for (size_t i = 0; i < num_indexes(); ++i) try { indexes_[i]->insert(row_id, datum); } catch (...) { for (size_t j = 0; j < i; ++i) { indexes_[j]->remove(row_id, datum); } throw; } values_[value_id] = new_value; }
// Return a value. // // If "row_id" is valid, returns the stored value. // If "row_id" is invalid, returns N/A. Int get(Int row_id) const { size_t value_id = row_id.raw(); if (value_id >= size_) { return Int::na(); } return _get(value_id); }
void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::true_ const&, mpl::false_ const&) { // Store. // Trivial. // Non binary. bool s = val.sign(); typename Int::local_limb_type l = *val.limbs(); ar & s; std::size_t limb_count = sizeof(l); ar & limb_count; for(std::size_t i = 0; i < limb_count; ++i) { unsigned char b = static_cast<unsigned char>(static_cast<typename Int::local_limb_type>(l >> (i * CHAR_BIT)) & static_cast<typename Int::local_limb_type>((1u << CHAR_BIT) - 1)); ar & b; } }
void Column<Float>::get(Int row_id, Datum *datum) const { size_t value_id = row_id.raw(); if (value_id >= values_.size()) { *datum = Float::na(); } else { *datum = values_[value_id]; } }