mathmaker  0.4(alpha)
mathmaker_dev/maintenance/autotest/obj_test/calc_test/items_test.py
00001 # -*- coding: utf-8 -*-
00002 
00003 # Mathmaker creates automatically maths exercises sheets
00004 # with their answers
00005 # Copyright 2006-2014 Nicolas Hainaux <nico_h@users.sourceforge.net>
00006 
00007 # This file is part of Mathmaker.
00008 
00009 # Mathmaker is free software; you can redistribute it and/or modify
00010 # it under the terms of the GNU General Public License as published by
00011 # the Free Software Foundation; either version 3 of the License, or
00012 # any later version.
00013 
00014 # Mathmaker is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 
00019 # You should have received a copy of the GNU General Public License
00020 # along with Mathmaker; if not, write to the Free Software
00021 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 import os
00024 import sys
00025 import locale
00026 
00027 from lib.common import default
00028 
00029 from core import *
00030 from core.base_calculus import *
00031 
00032 from maintenance.autotest import common
00033 
00034 try:
00035     locale.setlocale(locale.LC_ALL, default.LANGUAGE + '.' + default.ENCODING)
00036 except:
00037     locale.setlocale(locale.LC_ALL, '')
00038 
00039 check = common.check
00040 
00041 
00042 def action():
00043     if common.verbose:
00044         os.write(common.output, "--- ITEMS\n")
00045 
00046     item_1 = Item(1)
00047     item_minus_1 = Item(-1)
00048     item_minus_minus_1 = Item(('-', -1))
00049     item_a = Item('a')
00050     item_b = Item('b')
00051     item_minus_a = Item('-a')
00052     item_minus_minus_a = Item(('-', '-a'))
00053     item_minus_1_expon_item_minus_minus_1 = Item(('+',
00054                                                   -1,
00055                                                   item_minus_minus_1))
00056     item_minus_1_inside_expon_item_2 = Item(('+', -1, Item(2)))
00057 
00058     item_minus_1_expon_item_2 = Item(('-', 1, Item(2)))
00059 
00060     item_3_exponent_sum_minus_2_plus_6 = Item(('+', 3, Sum([-2, 6])))
00061 
00062     item_minus_3_inside_exponent_sum_minus_2_plus_5 = Item(('+',
00063                                                             -3,
00064                                                              Sum([-2, 5])
00065                                                            ))
00066 
00067     item_minus_5_inside_exponent_0 = Item(('+', -5, 0))
00068 
00069     item_2_power_minus_2_inside_power_4 = Item(('+', 2, Item(('+', -2, 4)) ))
00070 
00071     item_2_power_sum_minus_2_inside_power_4 = Item(('+',
00072                                                     2,
00073                                                     Sum([Item(('+', -2, 4))])
00074                                                    ))
00075 
00076     item_minus_2_inside_exponent_sum_1_and_0 = Item(('+', -2, Sum([1, 0]) ))
00077 
00078     item_minus_2_inside_exponent_sum_of_product_of_sum_1_and_0 = \
00079                                                Item(('+',
00080                                                      -2,
00081                                                      Sum([ Product([Sum([1, 0])
00082                                                                    ])
00083                                                          ])
00084                                                      ))
00085 
00086     item_minus_2_inside_exponent_sum_of_product_of_sum_1_and_1 = \
00087                                                Item(('+', -2,
00088                                                           Sum([
00089                                                           Product([Sum([1, 1])
00090                                                                   ])
00091                                                               ])
00092                                                      ))
00093 
00094     item_minus_2_inside_exponent_sum_of_sum_of_sum_1_and_1 = \
00095                                                Item(('+',
00096                                                      -2,
00097                                                       Sum([Sum([Sum([1, 1]) ])
00098                                                          ])
00099                                                     ))
00100 
00101     item_minus_2_inside_exponent_sum_of_sum_of_product_2_by_2 = \
00102                                                Item(('+',
00103                                                      -2,
00104                                                      Sum([Sum([Product([2, 2])
00105                                                                ])
00106                                                          ])
00107                                                     ))
00108 
00109     item_minus_2_inside_exponent_sum_of_product_of_sum_of_2 = \
00110                                                Item(('+', -2, Sum([Product([
00111                                                                        Sum([2])
00112                                                                           ])
00113                                                                   ])
00114                                                     ))
00115 
00116     item_6 = Item(6)
00117 
00118     item_to_round = Item(6.548)
00119 
00120     item_with_unit = Item(19.5)
00121     item_with_unit.set_unit('cm')
00122 
00123 
00124     #1
00125     check(item_1,
00126          ["1"])
00127 
00128     check(item_minus_1,
00129          ["-1"])
00130 
00131     check(item_minus_minus_1,
00132          ["-(-1)"])
00133 
00134     check(item_minus_minus_1.evaluate(),
00135          ["1"])
00136 
00137     #5
00138     check(item_a,
00139          ["a"])
00140 
00141     check(item_minus_a,
00142          ["-a"])
00143 
00144     check(item_minus_minus_a,
00145          ["-(-a)"])
00146 
00147     check(item_minus_1_expon_item_minus_minus_1,
00148          ["-1^{-(-1)}"])
00149 
00150     check(item_minus_1_expon_item_minus_minus_1.evaluate(),
00151          ["-1"])
00152 
00153     #10
00154     check(item_minus_1_inside_expon_item_2.is_numeric(),
00155          ["True"])
00156 
00157     check(item_minus_1_inside_expon_item_2.raw_value < 0,
00158          ["True"])
00159 
00160     check(item_minus_1_inside_expon_item_2.requires_inner_brackets(),
00161          ["True"])
00162 
00163     check(item_minus_1_inside_expon_item_2,
00164          ["(-1)^{2}"])
00165 
00166     check(item_minus_1_inside_expon_item_2.evaluate(),
00167          ["1"])
00168 
00169     #15
00170     check(item_minus_1_expon_item_2,
00171          ["-1^{2}"])
00172 
00173     check(item_minus_1_expon_item_2.evaluate(),
00174          ["-1"])
00175 
00176     check(item_3_exponent_sum_minus_2_plus_6,
00177          ["3^{-2+6}"])
00178 
00179     check(item_3_exponent_sum_minus_2_plus_6.evaluate(),
00180          ["81"])
00181 
00182     check(item_minus_3_inside_exponent_sum_minus_2_plus_5,
00183          ["(-3)^{-2+5}"])
00184 
00185     #20
00186     check(item_minus_3_inside_exponent_sum_minus_2_plus_5.evaluate(),
00187          ["-27"])
00188 
00189     check(item_minus_5_inside_exponent_0,
00190          ["1"])
00191 
00192     for i in xrange(len(common.machines)):
00193         test = common.machines[i].type_string(\
00194                                        item_minus_5_inside_exponent_0,
00195                                        force_display_exponent_0='OK')
00196         check(test, ["(-5)^{0}"])
00197 
00198     check(item_2_power_minus_2_inside_power_4,
00199          ["2^{(-2)^{4}}"])
00200 
00201     check(item_2_power_sum_minus_2_inside_power_4,
00202          ["2^{(-2)^{4}}"])
00203 
00204     #25 (will be shifted when adding a new machine kind)
00205     check(item_minus_2_inside_exponent_sum_1_and_0,
00206          ["-2"])
00207 
00208     check(item_minus_2_inside_exponent_sum_of_product_of_sum_1_and_0,
00209          ["-2"])
00210 
00211     check(item_minus_2_inside_exponent_sum_of_product_of_sum_1_and_1,
00212          ["(-2)^{1+1}"])
00213 
00214     item_minus_2_inside_exponent_sum_of_product_of_sum_1_and_1.exponent.term[
00215                                                   0].set_compact_display(False)
00216     check(item_minus_2_inside_exponent_sum_of_product_of_sum_1_and_1,
00217          ["(-2)^{1+1}"])
00218 
00219 
00220     check(item_minus_2_inside_exponent_sum_of_sum_of_sum_1_and_1,
00221          ["(-2)^{1+1}"])
00222 
00223     #30 (will be shifted when adding a new machine kind)
00224     check(item_minus_2_inside_exponent_sum_of_sum_of_product_2_by_2,
00225          ["(-2)^{2\\times 2}"])
00226 
00227     check(item_minus_2_inside_exponent_sum_of_product_of_sum_of_2,
00228          ["(-2)^{2}"])
00229 
00230     check(item_6.is_displ_as_a_single_1(),
00231          ["False"])
00232 
00233     check(item_to_round.digits_number(),
00234           ["3"])
00235 
00236     check(item_to_round.round(0),
00237           ["7"])
00238 
00239     check(item_to_round.round(1),
00240           [locale.str(6.5)])
00241 
00242     check(item_to_round.round(2),
00243           [locale.str(6.55)])
00244 
00245     check(item_to_round.round(3),
00246           [locale.str(6.548)])
00247 
00248     check(item_to_round.needs_to_get_rounded(0),
00249           ["True"])
00250 
00251     check(item_to_round.needs_to_get_rounded(1),
00252           ["True"])
00253 
00254     check(item_to_round.needs_to_get_rounded(2),
00255           ["True"])
00256 
00257     check(item_to_round.needs_to_get_rounded(3),
00258           ["False"])
00259 
00260     check(item_to_round.needs_to_get_rounded(4),
00261           ["False"])
00262 
00263     for i in xrange(len(common.machines)):
00264         test = common.machines[i].type_string(\
00265                                        item_with_unit,
00266                                        display_unit='yes')
00267         check(test, ["19,5 cm"])
00268 
00269 
00270