Esempio n. 1
0
pgsOperand pgsGenString::eval(pgsVarMap &vars) const
{
	// Evaluate parameters
	pgsOperand min(m_min->eval(vars));
	pgsOperand max(m_max->eval(vars));
	pgsOperand nb_words(m_nb_words->eval(vars));
	pgsOperand seed(m_seed->eval(vars));

	// Check parameters and create the generator
	if (min->is_integer() && max->is_integer() && nb_words->is_integer()
	        && seed->is_integer())
	{
		long aux_min, aux_max, aux_nb_words, aux_seed;
		min->value().ToLong(&aux_min);
		max->value().ToLong(&aux_max);
		nb_words->value().ToLong(&aux_nb_words);
		seed->value().ToLong(&aux_seed);
		return pnew pgsGenerator(pgsVariable::pgsTString,
		                         pnew pgsStringGen(aux_min, aux_max, aux_nb_words, 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 (!nb_words->is_integer())
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nnb_words should be an integer"));
		}
		else
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nseed should be an integer"));
		}
	}
}
Esempio n. 2
0
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);
	}
}
Esempio n. 3
0
pgsStringGen *pgsStringGen::clone()
{
	return pnew pgsStringGen(*this);
}
Esempio n. 4
0
pgsRegexGen::pgsRegexGen(const wxString &regex, const long &seed) :
	pgsObjectGen(seed), m_regex(regex), m_valid(true), m_string_gens(pgsVectorStringGen())
{
	wxLogScriptVerbose(wxT("REGEXGEN: %s"), m_regex.c_str());

	// Transform regular expression into XML structure
	bool escape = false, first_regex = true, list = false;
	wxString result = wxT("<regexpressions>\n");
	size_t i = 0;
	while (i < m_regex.Length())
	{
		if (escape)
		{
			if (list == true)
			{
				result.Append(espace_xml_char(m_regex[i]));
			}
			else
			{
				if (!first_regex)
					result.Append(wxT(" </regex>\n"));
				else
					first_regex = false;
				result.Append(wxT(" <regex>\n  <characters>"));
				result.Append(espace_xml_char(m_regex[i]));
				result.Append(wxT("</characters>\n"));
			}
			escape = false;
		}
		else if (list == true && m_regex[i] == wxT('-'))
		{
			if ((i + 1) < m_regex.Length())
			{
				result.Append(char_range(m_regex[i - 1], m_regex[i + 1]));
			}
		}
		else if (m_regex[i] == wxT('['))
		{
			if (!first_regex)
				result.Append(wxT(" </regex>\n"));
			else
				first_regex = false;
			result.Append(wxT(" <regex>\n  <characters>"));
			list = true;
		}
		else if (m_regex[i] == wxT(']'))
		{
			result.Append(wxT("</characters>\n"));
			list = false;
		}
		else if (m_regex[i] == wxT('{'))
		{
			result.Append(wxT("  <range>"));
			list = true;
		}
		else if (m_regex[i] == wxT('}'))
		{
			result.Append(wxT("</range>\n"));
			list = false;
		}
		else if (m_regex[i] == wxT('\\'))
		{
			escape = true;
		}
		else
		{
			if (list == true)
			{
				result.Append(espace_xml_char(m_regex[i]));
			}
			else
			{
				if (!first_regex)
					result.Append(wxT(" </regex>\n"));
				else
					first_regex = false;
				result.Append(wxT(" <regex>\n  <characters>"));
				result.Append(espace_xml_char(m_regex[i]));
				result.Append(wxT("</characters>\n"));
			}
		}

		++i;
	}
	if (result != wxT("<regexpressions>\n"))
		result.Append(wxT(" </regex>\n"));
	result.Append(wxT("</regexpressions>\n"));

	wxLogScriptVerbose(wxT("REGEXGEN: %s"), result.c_str());

	// Load this XML structure with the wxXmlDocument from wxWidgets
	wxStringInputStream input(result);
	wxXmlDocument doc;
	if (!doc.Load(input, wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES))
	{
		m_valid = false;
	}
	else
	{
		// Start processing the XML file
		if (doc.GetRoot()->GetName() != wxT("regexpressions"))
		{
			m_valid = false;
		}
		else
		{
			// Go through XML nodes
			wxXmlNode *xml_regexpressions = doc.GetRoot()->GetChildren();
			while (xml_regexpressions && m_valid)
			{
				if (xml_regexpressions->GetName() == wxT("regex"))
				{
					wxXmlNode *xml_regex = xml_regexpressions->GetChildren();

					pgsRegex regex;
					regex.set_first(1);

					while (xml_regex && m_valid)
					{
						if (xml_regex->GetName() == wxT("characters"))
						{
							wxString content = xml_regex->GetNodeContent();
							for (size_t i = 0; i < content.Length(); i++)
							{
								regex.add_character(content[i]);
							}
						}
						else if (xml_regex->GetName() == wxT("range"))
						{
							wxString content = xml_regex->GetNodeContent();
							wxRegEx ex(wxT("^([0-9]+)(,([0-9]+))?$"));
							wxASSERT(ex.IsValid());
							if (ex.Matches(content))
							{
								long min;
								ex.GetMatch(content, 1).ToLong(&min);
								regex.set_first(min);
								wxString smax = ex.GetMatch(content, 3);
								if (!smax.IsEmpty())
								{
									long max;
									smax.ToLong(&max);
									regex.set_second(max);
								}
							}
							else
							{
								// m_valid = false;
							}
						}

						xml_regex = xml_regex->GetNext();
					}

					m_string_gens.Add(pgsStringGen(regex.get_first(),
					                               regex.get_second(), 1, seed,
					                               regex.get_characters()));
				}

				xml_regexpressions = xml_regexpressions->GetNext();
			}
		}
	}
}