public function testIntegerToFloat()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection();
     $operands[] = new Integer(10);
     $processor = new IntegerToFloatProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
     $this->assertEquals(10.0, $result->getValue());
     $operands->reset();
     $operands[] = new Integer(-10);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
     $this->assertEquals(-10.0, $result->getValue());
     $operands->reset();
     $operands[] = new Integer(0);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
     $this->assertEquals(0.0, $result->getValue());
     $operands->reset();
     $operands[] = new Integer(-0);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
     $this->assertEquals(-0.0, $result->getValue());
 }
 public function testRound()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection();
     $operands[] = new QtiFloat(6.8);
     $processor = new RoundProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(7, $result->getValue());
     $operands->reset();
     $operands[] = new QtiFloat(6.5);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(7, $result->getValue());
     $operands->reset();
     $operands[] = new QtiFloat(6.49);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(6, $result->getValue());
     $operands->reset();
     $operands[] = new QtiFloat(6.5);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(7, $result->getValue());
     $operands->reset();
     $operands[] = new QtiFloat(-6.5);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(-6, $result->getValue());
     $operands->reset();
     $operands[] = new QtiFloat(-6.51);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(-7, $result->getValue());
     $operands->reset();
     $operands[] = new QtiFloat(-6.49);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(-6, $result->getValue());
     $operands->reset();
     $operands[] = new QtiInteger(0);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(0, $result->getValue());
     $operands->reset();
     $operands[] = new QtiFloat(-0.0);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(0, $result->getValue());
     $operands->reset();
     $operands[] = new QtiFloat(-0.5);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\QtiInteger', $result);
     $this->assertEquals(0, $result->getValue());
 }
 public function testNull()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection(array(new QtiInteger(10), null));
     $processor = new SubtractProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertSame(null, $result);
     $operands->reset();
     $operands[] = new MultipleContainer(BaseType::FLOAT);
     $result = $processor->process();
     $this->assertSame(null, $result);
 }
 public function testSize()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection();
     $operands[] = new MultipleContainer(BaseType::STRING, array(new String('String!')));
     $processor = new ContainerSizeProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertEquals(1, $result->getValue());
     $operands->reset();
     $operands[] = new MultipleContainer(BaseType::POINT, array(new Point(1, 2), new Point(2, 3), new Point(3, 4)));
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(3, $result->getValue());
 }
 public function testNull()
 {
     $expression = $this->createFakeExpression(false);
     $operands = new OperandsCollection();
     $operands[] = new String('test');
     $operands[] = null;
     $processor = new SubstringProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertSame(null, $result);
     $operands->reset();
     $operands[] = new String('');
     // in QTI, empty string considered to be NULL.
     $operands[] = new String('blah!');
     $result = $processor->process();
     $this->assertSame(null, $result);
 }
 public function testMixed()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection(array(new Integer(10), new Float(26.4), new Integer(-4), new Float(25.3)));
     $processor = new MinProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
     $this->assertEquals(-4.0, $result->getValue());
     $operands->reset();
     $operands[] = new OrderedContainer(BaseType::INTEGER, array(new Integer(2), new Integer(3), new Integer(1), new Integer(4), new Integer(5)));
     $operands[] = new Float(2.4);
     $operands[] = new MultipleContainer(BaseType::FLOAT, array(new Float(245.4), new Float(1337.1337)));
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
     $this->assertEquals(1.0, $result->getValue());
 }
 public function testNull()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection();
     // second operand is null.
     $operands[] = new QtiInteger(10);
     $operands[] = new OrderedContainer(BaseType::INTEGER);
     $processor = new MemberProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertSame(null, $result);
     // fist operand is null.
     $operands->reset();
     $operands[] = null;
     $operands[] = new MultipleContainer(BaseType::INTEGER, array(new QtiInteger(10)));
     $result = $processor->process();
     $this->assertSame(null, $result);
 }
 public function testNotEmpty()
 {
     $expression = $this->getFakeExpression();
     $operands = new OperandsCollection(array(new QtiInteger(0)));
     $processor = new IsNullProcessor($expression, $operands);
     $this->assertFalse($processor->process()->getValue());
     $operands->reset();
     $operands[] = new QtiBoolean(false);
     $this->assertFalse($processor->process()->getValue());
     $operands->reset();
     $operands[] = new QtiInteger(-1);
     $this->assertFalse($processor->process()->getValue());
     $operands->reset();
     $operands[] = new QtiPoint(1, 2);
     $this->assertFalse($processor->process()->getValue());
     $operands->reset();
     $operands[] = new MultipleContainer(BaseType::INTEGER, array(new QtiInteger(25)));
     $this->assertFalse($processor->process()->getValue());
     $operands->reset();
     $operands[] = new OrderedContainer(BaseType::POINT, array(new QtiPoint(3, 4), new QtiPoint(5, 6)));
     $this->assertFalse($processor->process()->getValue());
     $operands->reset();
     $operands[] = new RecordContainer(array('a' => new QtiBoolean(true), 'b' => null, 'c' => new QtiPoint(1, 2), 'd' => new QtiInteger(24), 'e' => new QtiFloat(23.3)));
     $this->assertFalse($processor->process()->getValue());
 }
 public function testNull()
 {
     // exp as a float is NaN when negative base is used.
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection(array(new Integer(-20), new Float(3.4)));
     $processor = new PowerProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertSame(null, $result);
     $operands->reset();
     $operands[] = new Integer(1);
     $operands[] = null;
     $result = $processor->process();
     $this->assertSame(null, $result);
     $operands->reset();
     $operands[] = new MultipleContainer(BaseType::FLOAT);
     $operands[] = new Integer(2);
     $result = $processor->process();
     $this->assertSame(null, $result);
 }
 public function testFile()
 {
     $fManager = new FileSystemFileManager();
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection();
     $file1 = $fManager->createFromData('Some text', 'text/plain');
     $file2 = $fManager->createFromData('Some text', 'text/plain');
     $operands[] = $file1;
     $operands[] = $file2;
     $processor = new MatchProcessor($expression, $operands);
     $this->assertTrue($processor->process()->getValue());
     $fManager->delete($file1);
     $fManager->delete($file2);
     $operands->reset();
     $file1 = $fManager->createFromData('Some text', 'text/plain');
     $file2 = $fManager->createFromData('Other text', 'text/plain');
     $operands[] = $file1;
     $operands[] = $file2;
     $this->assertFalse($processor->process()->getValue());
     $fManager->delete($file1);
     $fManager->delete($file2);
 }
Exemple #11
0
 public function testLte()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection();
     $operands[] = new Float(0.5);
     $operands[] = new Integer(1);
     $processor = new LteProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Boolean', $result);
     $this->assertTrue($result->getValue());
     $operands->reset();
     $operands[] = new Integer(1);
     $operands[] = new Float(0.5);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Boolean', $result);
     $this->assertFalse($result->getValue());
     $operands->reset();
     $operands[] = new Integer(1);
     $operands[] = new Integer(1);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Boolean', $result);
     $this->assertTrue($result->getValue());
 }
 /**
  * Process the current Expression object according to the current
  * execution context.
  * 
  * @throws ExpressionProcessingException|OperatorProcessingException If an error occurs during the Expression processing.
  */
 public function process()
 {
     $expression = $this->getComponent();
     // Reset trail and marker arrays.
     $this->trail = array();
     $this->marker = array();
     $this->pushTrail($expression);
     while (count($this->getTrail()) > 0) {
         $expression = $this->popTrail();
         if ($this->isMarked($expression) === false && $expression instanceof Operator) {
             // This is an operator, first pass. Repush for a second pass.
             $this->mark($expression);
             $this->pushTrail($expression);
             $this->pushTrail($expression->getExpressions());
         } else {
             if ($this->isMarked($expression)) {
                 // Operator, second pass. Process it.
                 $popCount = count($expression->getExpressions());
                 $operands = $this->operands->pop($popCount);
                 $processor = $this->operatorProcessorFactory->createProcessor($expression, $operands);
                 $processor->setState($this->getContext());
                 $result = $processor->process();
                 // trace the processing of the operator.
                 $qtiName = $expression->getQtiClassName();
                 $trace = "Operator '{$qtiName}' processed.";
                 $this->traceOperator($processor, $result);
                 if ($expression !== $this->getComponent()) {
                     $this->operands->push($result);
                 }
             } else {
                 // Simple expression, process it.
                 $processor = $this->expressionProcessorFactory->createProcessor($expression);
                 $processor->setState($this->getContext());
                 $result = $processor->process();
                 $this->operands->push($result);
                 // trace the processing of the expression.
                 $qtiName = $expression->getQtiClassName();
                 $trace = "Expression '{$qtiName}' processed.";
                 $this->traceExpression($processor, $result);
             }
         }
     }
     return $result;
 }
 public function testComplexMultipleInBetween()
 {
     $expression = $this->createFakeExpression();
     // For ordered containers [A,B,C,D] contains [B]
     $operands = new OperandsCollection();
     $operands[] = new MultipleContainer(BaseType::POINT, array(new Point(1, 2), new Point(3, 4), new Point(5, 6), new Point(7, 8)));
     $operands[] = new MultipleContainer(BaseType::POINT, array(new Point(3, 4)));
     $processor = new ContainsProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Boolean', $result);
     $this->assertTrue($result->getValue());
     // [A,B,C,D] does not contain [E]
     $operands->reset();
     $operands[] = new MultipleContainer(BaseType::POINT, array(new Point(1, 2), new Point(3, 4), new Point(5, 6), new Point(7, 8)));
     $operands[] = new MultipleContainer(BaseType::POINT, array(new Point(9, 10)));
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Boolean', $result);
     $this->assertFalse($result->getValue());
     // [A,B,C,D] contains [A,D]
     $operands->reset();
     $operands[] = new MultipleContainer(BaseType::POINT, array(new Point(1, 2), new Point(3, 4), new Point(5, 6), new Point(7, 8)));
     $operands[] = new MultipleContainer(BaseType::POINT, array(new Point(1, 2), new Point(7, 8)));
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Boolean', $result);
     $this->assertTrue($result->getValue());
 }
 public function testExclusivelyBoolean()
 {
     $operands = new OperandsCollection();
     $this->assertFalse($operands->exclusivelyBoolean());
     $operands[] = new QtiBoolean(true);
     $this->assertTrue($operands->exclusivelyBoolean());
     $operands[] = new QtiBoolean(false);
     $this->assertTrue($operands->exclusivelyBoolean());
     $container = new MultipleContainer(BaseType::BOOLEAN);
     $operands[] = $container;
     $this->assertFalse($operands->exclusivelyBoolean());
     $container[] = new QtiBoolean(false);
     $this->assertTrue($operands->exclusivelyBoolean());
     $operands = new OperandsCollection();
     $operands[] = new QtiBoolean(true);
     $this->assertTrue($operands->exclusivelyBoolean());
     $operands[] = null;
     $this->assertFalse($operands->exclusivelyBoolean());
     $operands = new OperandsCollection();
     $operands[] = new OrderedContainer(BaseType::BOOLEAN, array(new QtiBoolean(true), new QtiBoolean(false), new QtiBoolean(true)));
     $this->assertTrue($operands->exclusivelyBoolean());
     $operands[] = new MultipleContainer(BaseType::BOOLEAN);
     $this->assertFalse($operands->exclusivelyBoolean());
     $operands = new OperandsCollection();
     $operands[] = new QtiBoolean(true);
     $operands[] = new QtiBoolean(false);
     $this->assertTrue($operands->exclusivelyBoolean());
     $operands[] = new RecordContainer(array('b1' => new QtiBoolean(true), 'b2' => new QtiBoolean(false)));
     $operands = new OperandsCollection();
     $operands[] = new QtiBoolean(true);
     $operands[] = new MultipleContainer(BaseType::BOOLEAN, array(new QtiBoolean(true)));
     $this->assertTrue($operands->exclusivelyBoolean());
     $operands[] = new RecordContainer();
     $this->assertFalse($operands->exclusivelyBoolean());
 }
 public function testRound()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection();
     $operands[] = new Float(6.8);
     $processor = new TruncateProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(6, $result->getValue());
     $operands->reset();
     $operands[] = new Float(6.5);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(6, $result->getValue());
     $operands->reset();
     $operands[] = new Float(6.49);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(6, $result->getValue());
     $operands->reset();
     $operands[] = new Float(-6.5);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(-6, $result->getValue());
     $operands->reset();
     $operands[] = new Float(-6.8);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(-6, $result->getValue());
     $operands->reset();
     $operands[] = new Float(-6.49);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(-6, $result->getValue());
     $operands->reset();
     $operands[] = new Integer(0);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(0, $result->getValue());
     $operands->reset();
     $operands[] = new Float(-0.0);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(0, $result->getValue());
     $operands->reset();
     $operands[] = new Float(-0.5);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(0, $result->getValue());
     $operands->reset();
     $operands[] = new Float(-0.4);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(0, $result->getValue());
     $operands->reset();
     $operands[] = new Float(-0.6);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Integer', $result);
     $this->assertEquals(0, $result->getValue());
     $operands->reset();
     $operands[] = new Float(NAN);
     $result = $processor->process();
     $this->assertSame(null, $result);
     $operands->reset();
     $operands[] = new Float(-INF);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
     $this->assertEquals(-INF, $result->getValue());
     $operands->reset();
     $operands[] = new Float(INF);
     $result = $processor->process();
     $this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
     $this->assertEquals(INF, $result->getValue());
 }
 public function testNull()
 {
     $expression = $this->createFakeExpression();
     $operands = new OperandsCollection();
     $operands[] = null;
     $operands[] = new MultipleContainer(BaseType::INTEGER, array(new Integer(0), new Integer(10), new Integer(20), new Integer(30)));
     $processor = new DeleteProcessor($expression, $operands);
     $result = $processor->process();
     $this->assertSame(null, $result);
     $operands->reset();
     $operands[] = new Integer(10);
     $operands[] = new MultipleContainer(BaseType::INTEGER);
     $result = $processor->process();
     $this->assertSame(null, $result);
 }