pgsReferenceGen::pgsReferenceGen(pgsThread * app, const wxString & table, const wxString & column, const bool & sequence, const long & seed) : pgsObjectGen(seed), m_app(app), m_table(table), m_column(column), m_sequence(sequence) { // We need an empty symbol table for calling pgsExecute.eval(...) pgsVarMap vars; // Count the number of lines in the table pgsOperand result = pgsExecute(wxString() << wxT("SELECT count(*) FROM ") << m_table, 0, m_app).eval(vars); wxASSERT(result->is_record()); wxString value = result->value(); if (!value.IsEmpty()) { m_nb_rows = pgsMapm::pgs_str_mapm(result->number().value()); } else { m_nb_rows = 0; } wxLogScriptVerbose(wxT("REFGEN: Number of rows in %s: %s"), m_table.c_str(), pgsMapm::pgs_mapm_str(m_nb_rows).c_str()); // Create an integer generator with that number of lines m_randomizer = pgsRandomizer(pnew pgsIntegerGen(0, m_nb_rows - 1, is_sequence(), m_seed)); }
pgsDictionaryGen::pgsDictionaryGen(const wxString &file_path, const bool &sequence, const long &seed, wxCSConv conv) : pgsObjectGen(seed), m_file_path(file_path), m_conv(conv) { m_nb_lines = this->count_lines(); m_randomizer = pgsRandomizer(pnew pgsIntegerGen(1, m_nb_lines, sequence, seed)); }
pgsDateTimeGen::pgsDateTimeGen(wxDateTime min, wxDateTime max, const bool & sequence, const long & seed) : pgsObjectGen(seed), m_min(min.IsEarlierThan(max) || min.IsEqualTo(max) ? min : max), m_max(max.IsLaterThan(min) || max.IsEqualTo(min) ? max : min), m_range(m_max.Subtract(m_min).GetSeconds()), m_sequence(sequence) { m_randomizer = pgsRandomizer(pnew pgsIntegerGen(0, std::string(m_range .ToString().mb_str()).c_str(), is_sequence(), m_seed)); }
pgsTimeGen::pgsTimeGen(wxDateTime min, wxDateTime max, const bool & sequence, const long & seed) : pgsObjectGen(seed), m_min(min.IsEarlierThan(max) || min.IsEqualTo(max) ? min : max), m_max(max.IsLaterThan(min) || max.IsEqualTo(min) ? max : min), m_range(m_max.Subtract(m_min).GetSeconds()), m_sequence(sequence) { m_min.SetYear(1970); // We know this date is not a DST date m_min.SetMonth(wxDateTime::Jan); m_min.SetDay(1); m_randomizer = pgsRandomizer(pnew pgsIntegerGen(0, std::string(m_range .ToString().mb_str()).c_str(), is_sequence(), m_seed)); }
pgsStringGen::pgsStringGen(USHORT w_size_min, USHORT w_size_max, const UCHAR &nb_words, const long &seed, pgsVectorChar characters) : pgsObjectGen(seed), m_nb_words(nb_words), m_characters(characters) { init_characters(); // Initialize vector if it is empty const USHORT w_size_aux = w_size_min; w_size_min = wxMin(w_size_aux, w_size_max); w_size_max = wxMax(w_size_aux, w_size_max); size_t char_count = m_characters.GetCount(); m_w_size_randomizer = pgsRandomizer(pnew pgsIntegerGen(w_size_min, w_size_max, false, seed)); m_letter_randomizer = pgsRandomizer(pnew pgsIntegerGen(0, wx_static_cast(long, char_count) - 1, false, seed)); }
pgsOperand pgsGenInt::eval(pgsVarMap &vars) const { // Evaluate parameters pgsOperand min(m_min->eval(vars)); pgsOperand max(m_max->eval(vars)); pgsOperand sequence(m_sequence->eval(vars)); pgsOperand seed(m_seed->eval(vars)); // Check parameters and create the generator if (min->is_integer() && max->is_integer() && sequence->is_integer() && seed->is_integer()) { long aux_sequence, aux_seed; sequence->value().ToLong(&aux_sequence); seed->value().ToLong(&aux_seed); return pnew pgsGenerator(pgsVariable::pgsTInt, pnew pgsIntegerGen(pgsVariable::num(min), pgsVariable::num(max), aux_sequence != 0, aux_seed)); } else { // Deal with errors if (!min->is_integer()) { throw pgsParameterException(wxString() << value() << wxT(":\nmin should be an integer")); } else if (!max->is_integer()) { throw pgsParameterException(wxString() << value() << wxT(":\nmax should be an integer")); } else if (!sequence->is_integer()) { throw pgsParameterException(wxString() << value() << wxT(":\nsequence should be an integer")); } else { throw pgsParameterException(wxString() << value() << wxT(":\nseed should be an integer")); } } }
void pgsTestSuite::test_operator_number(void) { // From a number with a string { // [1] Create variables pgsVariable * var0 = 0; var0 = pnew pgsNumber(wxT("100")); pgsVariable * var1 = 0; var1 = pnew pgsString(wxT("5")); // [2] Addition try { (*var0 + *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [3] Subtraction try { (*var0 - *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [4] Multiplication try { (*var0 * *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [5] Division try { (*var0 / *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [6] Modulo try { (*var0 % *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [9] Equal try { (*var0 == *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [10] Different try { (*var0 != *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [11] Lower try { (*var0 < *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [12] Greater try { (*var0 > *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [13] Lower or equal try { (*var0 <= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [14] Greater or equal try { (*var0 >= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [15] Not TS_ASSERT((!(*var0))->value() == wxT("0")); pgsOperand op(pnew pgsNumber(wxT("0"))); TS_ASSERT((!(*op))->value() == wxT("1")); // [16] Almost equal try { (*var0 &= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [17] Is true? TS_ASSERT(var0->pgs_is_true() == true); TS_ASSERT((!(*var0))->pgs_is_true() == false); // [18] Delete variables pdelete(var0); pdelete(var1); } // From a number (integer) with a real number { // [1] Create variables pgsVariable * var0 = 0; var0 = pnew pgsNumber(wxT("100"), pgsInt); pgsVariable * var1 = 0; var1 = pnew pgsNumber(wxT("5"), pgsReal); // [2] Addition TS_ASSERT((*var0 + *var1)->value() == wxT("105")); // [3] Subtraction TS_ASSERT((*var0 - *var1)->value() == wxT("95")); // [4] Multiplication TS_ASSERT((*var0 * *var1)->value() == wxT("500")); // [5] Division TS_ASSERT((*var0 / *var1)->value() == wxT("20")); // [6] Modulo TS_ASSERT((*var0 % *var1)->value() == wxT("0")); // [9] Equal TS_ASSERT((*var0 == *var1)->value() == wxT("0")); // [10] Different TS_ASSERT((*var0 != *var1)->value() == wxT("1")); // [11] Lower TS_ASSERT((*var0 < *var1)->value() == wxT("0")); // [12] Greater TS_ASSERT((*var0 > *var1)->value() == wxT("1")); // [13] Lower or equal TS_ASSERT((*var0 <= *var1)->value() == wxT("0")); // [14] Greater or equal TS_ASSERT((*var0 >= *var1)->value() == wxT("1")); // [15] Not TS_ASSERT((!(*var0))->value() == wxT("0")); // [16] Almost equal TS_ASSERT((*var0 &= *var1)->value() == wxT("0")); // [17] Is true? TS_ASSERT(var0->pgs_is_true() == true); TS_ASSERT((!(*var0))->pgs_is_true() == false); // [18] Delete variables pdelete(var0); pdelete(var1); } // From a number with a record { // [1] Create variables pgsVariable * var0 = 0; var0 = pnew pgsNumber(wxT("100")); pgsRecord * rec = 0; rec = pnew pgsRecord(1); rec->insert(0, 0, pnew pgsString(wxT("5"))); pgsVariable * var1 = 0; var1 = rec; // [2] Addition try { (*var0 + *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [3] Subtraction try { (*var0 - *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [4] Multiplication try { (*var0 * *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [5] Division try { (*var0 / *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [6] Modulo try { (*var0 % *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [9] Equal try { (*var0 == *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [10] Different try { (*var0 != *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [11] Lower try { (*var0 < *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [12] Greater try { (*var0 > *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [13] Lower or equal try { (*var0 <= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [14] Greater or equal try { (*var0 >= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [15] Not TS_ASSERT((!(*var0))->value() == wxT("0")); // [16] Almost equal try { (*var0 &= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [17] Is true? TS_ASSERT(var0->pgs_is_true() == true); TS_ASSERT((!(*var0))->pgs_is_true() == false); // [18] Delete variables pdelete(var0); pdelete(var1); } // From a number with a record { // [1] Create variables pgsVariable * var0 = 0; var0 = pnew pgsNumber(wxT("100")); pgsRecord * rec = 0; rec = pnew pgsRecord(1); rec->insert(0, 0, pnew pgsNumber(wxT("5"))); rec->insert(1, 0, pnew pgsNumber(wxT("1"))); pgsVariable * var1 = 0; var1 = rec; // [2] Addition try { (*var0 + *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [3] Subtraction try { (*var0 - *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [4] Multiplication try { (*var0 * *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [5] Division try { (*var0 / *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [6] Modulo try { (*var0 % *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [9] Equal try { (*var0 == *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [10] Different try { (*var0 != *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [11] Lower try { (*var0 < *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [12] Greater try { (*var0 > *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [13] Lower or equal try { (*var0 <= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [14] Greater or equal try { (*var0 >= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [18] Delete variables pdelete(var0); pdelete(var1); } // From a number with a string generator { // [1] Create variables pgsVariable * var0 = 0; var0 = pnew pgsNumber(wxT("100")); pgsStringGen * gen = 0; gen = pnew pgsStringGen(10, 20); pgsVariable * var1 = 0; var1 = pnew pgsGenerator(pgsVariable::pgsTString, gen); // [2] Addition try { (*var0 + *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [3] Subtraction try { (*var0 - *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [4] Multiplication try { (*var0 * *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [5] Division try { (*var0 / *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [6] Modulo try { (*var0 % *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [9] Equal try { (*var0 == *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [10] Different try { (*var0 != *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [11] Lower try { (*var0 < *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [12] Greater try { (*var0 > *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [13] Lower or equal try { (*var0 <= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [14] Greater or equal try { (*var0 >= *var1); TS_ASSERT(false); } catch (const pgsException &) { } // [18] Delete variables pdelete(var0); pdelete(var1); } // From a number with an integer generator { // [1] Create variables pgsVariable * var0 = 0; var0 = pnew pgsNumber(wxT("100")); pgsIntegerGen * gen = 0; gen = pnew pgsIntegerGen(5, 5); pgsVariable * var1 = 0; var1 = pnew pgsGenerator(pgsVariable::pgsTInt, gen); // [2] Addition TS_ASSERT((*var0 + *var1)->value() == wxT("105")); // [3] Subtraction TS_ASSERT((*var0 - *var1)->value() == wxT("95")); // [4] Multiplication TS_ASSERT((*var0 * *var1)->value() == wxT("500")); // [5] Division TS_ASSERT((*var0 / *var1)->value() == wxT("20")); // [6] Modulo TS_ASSERT((*var0 % *var1)->value() == wxT("0")); // [9] Equal TS_ASSERT((*var0 == *var1)->value() == wxT("0")); // [10] Different TS_ASSERT((*var0 != *var1)->value() == wxT("1")); // [11] Lower TS_ASSERT((*var0 < *var1)->value() == wxT("0")); // [12] Greater TS_ASSERT((*var0 > *var1)->value() == wxT("1")); // [13] Lower or equal TS_ASSERT((*var0 <= *var1)->value() == wxT("0")); // [14] Greater or equal TS_ASSERT((*var0 >= *var1)->value() == wxT("1")); // [15] Not TS_ASSERT((!(*var0))->value() == wxT("0")); // [16] Almost equal TS_ASSERT((*var0 &= *var1)->value() == wxT("0")); // [17] Is true? TS_ASSERT(var0->pgs_is_true() == true); TS_ASSERT((!(*var0))->pgs_is_true() == false); // [18] Delete variables pdelete(var0); pdelete(var1); } }