Blame view

Giac_maj/epsilon-giac/poincare/test/helper.cpp 2.12 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include <quiz.h>
  #include <poincare.h>
  #include <string.h>
  #include <ion.h>
  #include <stdlib.h>
  #include <assert.h>
  #include <cmath>
  
  using namespace Poincare;
  
  static Expression * parse_expression(const char * expression) {
    quiz_print(expression);
    char buffer[200];
    strlcpy(buffer, expression, sizeof(buffer));
    for (char *c = buffer; *c; c++) {
      switch (*c) {
        case 'E': *c = Ion::Charset::Exponent; break;
        case 'X': *c = Ion::Charset::Exponential; break;
        case 'I': *c = Ion::Charset::IComplex; break;
        case 'R': *c = Ion::Charset::Root; break;
        case 'P': *c = Ion::Charset::SmallPi; break;
      }
    }
    Expression * result = Expression::parse(buffer);
    assert(result);
    return result;
  }
  
  void assert_parsed_expression_type(const char * expression, Poincare::Expression::Type type) {
    Expression * e = parse_expression(expression);
    assert(e->type() == type);
    delete e;
  }
  
  #if POINCARE_SIMPLIFY
  void assert_parsed_simplified_expression_type(const char * expression, Poincare::Expression::Type type) {
    Expression * e = parse_expression(expression);
    Expression * e2 = e->simplify();
    assert(e2);
    assert(e2->type() == type);
    delete e;
    delete e2;
  }
  #endif
  
  template<typename T>
  void assert_parsed_expression_evaluates_to(const char * expression, Complex<T> * results, int numberOfRows, int numberOfColumns, Expression::AngleUnit angleUnit) {
    GlobalContext globalContext;
    Expression * a = parse_expression(expression);
    Evaluation<T> * m = a->evaluate<T>(globalContext, angleUnit);
    assert(m);
    assert(m->numberOfRows() == numberOfRows);
    assert(m->numberOfColumns() == numberOfColumns);
    for (int i = 0; i < m->numberOfOperands(); i++) {
      assert(std::fabs(m->complexOperand(i)->a() - results[i].a()) < 0.0001f);
      assert(std::fabs(m->complexOperand(i)->b() - results[i].b()) < 0.0001f);
    }
    delete a;
    delete m;
  }
  
  template void assert_parsed_expression_evaluates_to<float>(char const*, Poincare::Complex<float>*, int, int, Poincare::Expression::AngleUnit);
  template void assert_parsed_expression_evaluates_to<double>(char const*, Poincare::Complex<double>*, int, int, Poincare::Expression::AngleUnit);