Add remark about complex conjugation.
[ginac.git] / check / exam_numeric.cpp
index 7a7c603c6a8664d952ea26d0d411ed4da5a4e694..90e435e8f53f03243ee9723325f848f269012b64 100644 (file)
@@ -4,7 +4,7 @@
  *  tests on these numbers like is_integer() etc... */
 
 /*
  *  tests on these numbers like is_integer() etc... */
 
 /*
- *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 
 #include "exams.h"
 
 
 #include "exams.h"
 
+#include <sstream>
+
 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
  * conversions. */
 /* Simple and maybe somewhat pointless consistency tests of assorted tests and
  * conversions. */
-static unsigned exam_numeric1(void)
+static unsigned exam_numeric1()
 {
        unsigned result = 0;
        numeric test_int1(42);
 {
        unsigned result = 0;
        numeric test_int1(42);
@@ -79,7 +81,7 @@ static unsigned exam_numeric1(void)
        }
        
        e2 = test_int1 + a;
        }
        
        e2 = test_int1 + a;
-       if (ex_to<numeric>(e2).is_integer()) {
+       if (e2.info(info_flags::integer)) {
                clog << "expression " << e2
                     << " erroneously recognized as integer" << endl;
                ++result;
                clog << "expression " << e2
                     << " erroneously recognized as integer" << endl;
                ++result;
@@ -110,7 +112,7 @@ static unsigned exam_numeric1(void)
  * Implementing a workaround sadly introduced another bug on May 28th 1999
  * that was fixed on May 31st.  The workaround turned out to be stupid and
  * the original bug in CLN was finally killed on September 2nd. */
  * Implementing a workaround sadly introduced another bug on May 28th 1999
  * that was fixed on May 31st.  The workaround turned out to be stupid and
  * the original bug in CLN was finally killed on September 2nd. */
-static unsigned exam_numeric2(void)
+static unsigned exam_numeric2()
 {
        unsigned result = 0;
        
 {
        unsigned result = 0;
        
@@ -149,7 +151,7 @@ static unsigned exam_numeric2(void)
 
 /* Assorted tests to ensure some crucial functions behave exactly as specified
  * in the documentation. */
 
 /* Assorted tests to ensure some crucial functions behave exactly as specified
  * in the documentation. */
-static unsigned exam_numeric3(void)
+static unsigned exam_numeric3()
 {
        unsigned result = 0;
        numeric calc_rem, calc_quo;
 {
        unsigned result = 0;
        numeric calc_rem, calc_quo;
@@ -267,7 +269,7 @@ static unsigned exam_numeric3(void)
 
 /* Now we perform some less trivial checks about several functions which should
  * return exact numbers if possible. */
 
 /* Now we perform some less trivial checks about several functions which should
  * return exact numbers if possible. */
-static unsigned exam_numeric4(void)
+static unsigned exam_numeric4()
 {
        unsigned result = 0;
        bool passed;
 {
        unsigned result = 0;
        bool passed;
@@ -298,7 +300,7 @@ static unsigned exam_numeric4(void)
 
 /* This test examines that simplifications of the form 5^(3/2) -> 5*5^(1/2)
  * are carried out properly. */
 
 /* This test examines that simplifications of the form 5^(3/2) -> 5*5^(1/2)
  * are carried out properly. */
-static unsigned exam_numeric5(void)
+static unsigned exam_numeric5()
 {
        unsigned result = 0;
        
 {
        unsigned result = 0;
        
@@ -315,7 +317,63 @@ static unsigned exam_numeric5(void)
        return result;
 }
 
        return result;
 }
 
-unsigned exam_numeric(void)
+/* This test checks whether the numeric output/parsing routines are
+   consistent. */
+static unsigned exam_numeric6()
+{
+       unsigned result = 0;
+
+       symbol sym("sym");
+       vector<ex> test_numbers;
+       test_numbers.push_back(numeric(0));                     // zero
+       test_numbers.push_back(numeric(1));                     // one
+       test_numbers.push_back(numeric(-1));            // minus one
+       test_numbers.push_back(numeric(42));            // positive integer
+       test_numbers.push_back(numeric(-42));           // negative integer
+       test_numbers.push_back(numeric(14,3));          // positive rational
+       test_numbers.push_back(numeric(-14,3));         // negative rational
+       test_numbers.push_back(numeric(3.141));         // positive decimal
+       test_numbers.push_back(numeric(-3.141));        // negative decimal
+       test_numbers.push_back(numeric(0.1974));        // positive decimal, leading zero
+       test_numbers.push_back(numeric(-0.1974));       // negative decimal, leading zero
+       test_numbers.push_back(sym);                            // symbol
+
+       for (vector<ex>::const_iterator br=test_numbers.begin(); br<test_numbers.end(); ++br) {
+               for (vector<ex>::const_iterator bi=test_numbers.begin(); bi<test_numbers.end(); ++bi) {
+
+                       for (vector<ex>::const_iterator er=test_numbers.begin(); er<test_numbers.end(); ++er) {
+                               for (vector<ex>::const_iterator ei=test_numbers.begin(); ei<test_numbers.end(); ++ei) {
+
+                                       // Construct expression, don't test invalid ones
+                                       ex base = (*br) + (*bi)*I, exponent = (*er) + (*ei)*I, x;
+                                       try {
+                                               x = pow(base, exponent);
+                                       } catch (...) {
+                                               continue;
+                                       }
+
+                                       // Print to string
+                                       std::ostringstream s;
+                                       s << x;
+
+                                       // Read back expression from string
+                                       string x_as_string = s.str();
+                                       ex x_again(x_as_string, lst(sym));
+
+                                       // They should be equal
+                                       if (!x_again.is_equal(x)) {
+                                               clog << x << " was read back as " << x_again << endl;
+                                               ++result;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       return result;
+}
+
+unsigned exam_numeric()
 {
        unsigned result = 0;
        
 {
        unsigned result = 0;
        
@@ -327,6 +385,7 @@ unsigned exam_numeric(void)
        result += exam_numeric3();  cout << '.' << flush;
        result += exam_numeric4();  cout << '.' << flush;
        result += exam_numeric5();  cout << '.' << flush;
        result += exam_numeric3();  cout << '.' << flush;
        result += exam_numeric4();  cout << '.' << flush;
        result += exam_numeric5();  cout << '.' << flush;
+       result += exam_numeric6();  cout << '.' << flush;
        
        if (!result) {
                cout << " passed " << endl;
        
        if (!result) {
                cout << " passed " << endl;