Date: prev next · Thread: first prev next last
2011 Archives by date, by thread · List index


Hi Jonas,

Really sorry for not giving this any attention for so long.

I've noticed other stuff I've messed up though, so I'm going to continue
on this some more :)
Again I'm not sure you messed it up... It might as well have been
messed up the first time I wrote it...
But don't let that stop you from fixing it...

I'm pretty sure I messed some of it up quite badly, I've attached a
patch that adds some of the brackets back in.

Removing the brackets from the SmOperNodes was a pretty bad, so they can
just go back in.

Removing them from UnHor and BinHor also broke some stuff, however, I
didn't want to add them back in here, so decided that I would add them
with the other nodes types that they were breaking with which seemed to
be BinVer nodes (also the BinDiagonal which is similar).

Some of the tests are the same kind as the one from last time. It can be
a bit difficult to get the right node tree for the test from the parser,
which also seems to put in more expression nodes than doing it with the
cursor, which confused me for a bit.

Regards,
Luke


From 124f5c0f593f477ca30014f7b7ac0be977292895 Mon Sep 17 00:00:00 2001
From: Luke Dixon <6b8b4567@gmail.com>
Date: Sat, 29 Jan 2011 12:32:53 +0000
Subject: [PATCH] Add some brackets back to SmNodeToTextVisitor

For SmOperNodes, I shouldn't have removed these brackets.
Add brackets around SmBinVerNodes and SmBinDiagonalNodes, removing the ones
from SmBinHorNodes and SmUnHorNodes made these ones necessary.
Add brackets when an SmExpressionNode has an SmBinHorNode child.
---
 starmath/qa/cppunit/test_nodetotextvisitors.cxx |  167 ++++++++++++++++++++++-
 starmath/source/visitors.cxx                    |   24 +++-
 2 files changed, 181 insertions(+), 10 deletions(-)

diff --git a/starmath/qa/cppunit/test_nodetotextvisitors.cxx 
b/starmath/qa/cppunit/test_nodetotextvisitors.cxx
index 687f75b..177bb82 100644
--- a/starmath/qa/cppunit/test_nodetotextvisitors.cxx
+++ b/starmath/qa/cppunit/test_nodetotextvisitors.cxx
@@ -93,6 +93,9 @@ namespace {
 
 class Test : public CppUnit::TestFixture {
 public:
+    Test();
+    ~Test();
+
     // init
     virtual void setUp();
     virtual void tearDown();
@@ -111,6 +114,9 @@ public:
     void SimpleGreekChars();
     void SimpleSpecialChars();
     void testBinomInBinHor();
+    void testBinVerInUnary();
+    void testBinHorInSubSup();
+    void testUnaryInMixedNumberAsNumerator();
 
     CPPUNIT_TEST_SUITE(Test);
     CPPUNIT_TEST(SimpleUnaryOp);
@@ -126,15 +132,19 @@ public:
     CPPUNIT_TEST(SimpleGreekChars);
     CPPUNIT_TEST(SimpleSpecialChars);
     CPPUNIT_TEST(testBinomInBinHor);
+    CPPUNIT_TEST(testBinVerInUnary);
+    CPPUNIT_TEST(testBinHorInSubSup);
+    CPPUNIT_TEST(testUnaryInMixedNumberAsNumerator);
     CPPUNIT_TEST_SUITE_END();
 
 private:
     uno::Reference< uno::XComponentContext > m_context;
     SmDocShellRef xDocShRef;
     void parseandparseagain(const char *input, const char *test_name);
+    void ParseAndCheck(const char *input, const char *expected, const char *test_name);
 };
 
-void Test::setUp()
+Test::Test()
 {
     m_context = cppu::defaultBootstrap_InitialComponentContext();
 
@@ -149,12 +159,20 @@ void Test::setUp()
     InitVCL(xSM);
 
     SmDLL::Init();
+}
 
+void Test::setUp()
+{
     xDocShRef = new SmDocShell(SFXOBJECTSHELL_STD_NORMAL);
 }
 
 void Test::tearDown()
 {
+    xDocShRef.Clear();
+}
+
+Test::~Test()
+{
 }
 
 /*
@@ -172,6 +190,17 @@ void Test::SimpleUnaryOp()
     parseandparseagain("+-3", "Plus/minus");
     parseandparseagain("-+4", "Minus/plus");
     parseandparseagain("neg a", "Boolean 'not'");
+    parseandparseagain("fact a", "Factorial");
+    parseandparseagain(" - { 1 over 2 } ", "BinVer in Unary 1");
+    ParseAndCheck(" - { 1 over 2 } ", " - { 1 over 2 } ", "BinVer in Unary 1");
+    parseandparseagain(" { - { 1 over 2 } } ", "BinVer in Unary 2");
+    parseandparseagain(" - 1 over 2 ", "Unary in BinVer as numerator 1");
+    parseandparseagain(" { - 1 } over 2 ", "Unary in BinVer as numerator 2");
+    parseandparseagain(" 1 over - 2 ", "Unary in BinVer as denominator 1");
+    parseandparseagain(" 1 over { - 2 } ", "Unary in BinVer as denominator 2");
+    parseandparseagain(" 2 { - 1 over 2 } ", "Mixed number with Unary in denominator 1");
+    parseandparseagain(" 2 { - 1 } over 2 ", "Mixed number with Unary in denominator 2");
+    parseandparseagain(" - 1 + 2 ", "Unary in BinHor");
 }
 
 void Test::SimpleBinaryOp()
@@ -266,7 +295,6 @@ void Test::SimpleFunctions()
     parseandparseagain("arcosh(a)", "Arc hyperbolic cosine");
     parseandparseagain("artanh(a)", "Arc hyperbolic tangent");
     parseandparseagain("arcoth(a)", "Arc hyperbolic cotangent");
-    parseandparseagain("fact a", "Factorial");
 }
 
 void Test::SimpleOperators()
@@ -275,7 +303,9 @@ void Test::SimpleOperators()
     parseandparseagain("sum{a}", "Sum");
     parseandparseagain("prod{a}", "Product");
     parseandparseagain("coprod{a}", "Coproduct");
-    parseandparseagain("int from {r_0} to {r_t} a", "Upper and lower bounds shown with integral");
+    parseandparseagain("int from {r_0} to {r_t} a", "Upper and lower bounds shown with integral 
(from & to)");
+    ParseAndCheck("int csup {r_0} csub {r_t} a", "int csup { r rsub 0 } csub { r rsub t } a ", 
"Upper and lower bounds shown with integral (csub & csup)");
+    ParseAndCheck("sum csup { size 8 { x - 1 } } csub { size 8 a } b ", "sum csup { size 8 { x - 1 
} } csub { size 8 a } b ", "Sum with sized upper and lower bounds");
     parseandparseagain("int{a}", "Integral");
     parseandparseagain("iint{a}", "Double integral");
     parseandparseagain("iiint{a}", "Triple integral");
@@ -497,6 +527,26 @@ void Test::parseandparseagain(const char *formula, const char *test_name)
     delete pNode2;
 }
 
+void Test::ParseAndCheck(const char *formula, const char * expected, const char *test_name)
+{
+    String sInput, sOutput, sExpected;
+    SmNode *pNode;
+
+    // parse
+    sInput.AppendAscii(formula);
+    pNode = SmParser().ParseExpression(sInput);
+    pNode->Prepare(xDocShRef->GetFormat(), *xDocShRef);
+    SmNodeToTextVisitor(pNode, sOutput);
+
+    // compare
+    sExpected.AppendAscii(expected);
+    CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name,
+        sExpected,
+        sOutput);
+
+    delete pNode;
+}
+
 void Test::testBinomInBinHor()
 {
     String sInput, sExpected, sOutput;
@@ -521,6 +571,117 @@ void Test::testBinomInBinHor()
 
     sExpected.AppendAscii(" { { binom a b + c } + d } ");
     CPPUNIT_ASSERT_EQUAL_MESSAGE("Binom Node in BinHor Node", sExpected, xDocShRef->GetText());
+
+    delete pTree;
+}
+
+void Test::testBinVerInUnary()
+{
+    String sInput, sExpected, sOutput;
+    SmNode* pTree;
+
+    // set up a unary operator with operand
+    sInput.AppendAscii("- 1");
+    pTree = SmParser().Parse(sInput);
+    pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef);
+
+    SmCursor aCursor(pTree, xDocShRef);
+    TestOutputDevice aOutputDevice;
+
+    // move forward (more than) enough places to be at the end
+    int i;
+    for (i = 0; i < 3; ++i)
+        aCursor.Move(&aOutputDevice, MoveRight);
+
+    // select the operand
+    aCursor.Move(&aOutputDevice, MoveLeft, false);
+    // set up a fraction
+    aCursor.InsertFraction();
+    aCursor.Move(&aOutputDevice, MoveDown);
+    aCursor.InsertText('2');
+
+    sExpected.AppendAscii(" - { 1 over 2 } ");
+    CPPUNIT_ASSERT_EQUAL_MESSAGE("Binary Vertical in Unary Operator", sExpected, 
xDocShRef->GetText());
+
+    delete pTree;
+}
+
+void Test::testBinHorInSubSup()
+{
+    String sInput, sExpected, sOutput;
+    SmNode* pTree;
+
+    // set up a blank formula
+    sInput.AppendAscii("");
+    pTree = SmParser().Parse(sInput);
+    pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef);
+
+    SmCursor aCursor(pTree, xDocShRef);
+    TestOutputDevice aOutputDevice;
+
+    // Insert an RSup expression with a BinHor for the exponent
+    aCursor.InsertText('a');
+    aCursor.InsertSubSup(RSUP);
+    aCursor.InsertText('b');
+    aCursor.InsertElement(PlusElement);
+    aCursor.InsertText('c');
+
+    // Move to the end and add d to the expression
+    aCursor.Move(&aOutputDevice, MoveRight);
+    aCursor.InsertElement(PlusElement);
+    aCursor.InsertText('d');
+
+    sExpected.AppendAscii(" { a rsup { b + c } + d } ");
+    CPPUNIT_ASSERT_EQUAL_MESSAGE("BinHor in SubSup", sExpected, xDocShRef->GetText());
+
+    delete pTree;
+}
+
+void Test::testUnaryInMixedNumberAsNumerator()
+{
+    String sInput, sExpected, sOutput;
+    SmNode* pTree;
+
+    // set up a unary operator
+    sInput.AppendAscii("- 1");
+    pTree = SmParser().Parse(sInput);
+    pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef);
+
+    SmCursor aCursor(pTree, xDocShRef);
+    TestOutputDevice aOutputDevice;
+
+    // move forward (more than) enough places to be at the end
+    int i;
+    for (i = 0; i < 3; ++i)
+        aCursor.Move(&aOutputDevice, MoveRight);
+
+    // Select the whole Unary Horizontal Node
+    aCursor.Move(&aOutputDevice, MoveLeft, false);
+    aCursor.Move(&aOutputDevice, MoveLeft, false);
+
+    // Set up a fraction
+    aCursor.InsertFraction();
+    aCursor.Move(&aOutputDevice, MoveDown);
+    aCursor.InsertText('2');
+
+    // Move left and turn this into a mixed number
+    // (bad form, but this could happen right?)
+    aCursor.Move(&aOutputDevice, MoveLeft);
+    aCursor.Move(&aOutputDevice, MoveLeft);
+    aCursor.InsertText('2');
+
+    // move forward (more than) enough places to be at the end
+    for (i = 0; i < 8; ++i)
+        aCursor.Move(&aOutputDevice, MoveRight);
+
+    // add 4 to the end
+    aCursor.InsertElement(PlusElement);
+    aCursor.InsertText('4');
+
+    sExpected.AppendAscii(" { 2 { - 1 over 2 } + 4 } ");
+    CPPUNIT_ASSERT_EQUAL_MESSAGE("Unary in mixed number as Numerator", sExpected, 
xDocShRef->GetText());
+
+    delete pTree;
 }
 
 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
diff --git a/starmath/source/visitors.cxx b/starmath/source/visitors.cxx
index 6ce8112..9d0a8c5 100644
--- a/starmath/source/visitors.cxx
+++ b/starmath/source/visitors.cxx
@@ -2244,33 +2244,39 @@ void SmNodeToTextVisitor::Visit( SmOperNode* pNode )
         SmNode* pChild;
         if( ( pChild = pSubSup->GetSubSup( LSUP ) ) ) {
             Separate( );
-            Append( "lsup " );
+            Append( "lsup { " );
             LineToText( pChild );
+            Append( "} " );
         }
         if( ( pChild = pSubSup->GetSubSup( LSUB ) ) ) {
             Separate( );
-            Append( "lsub " );
+            Append( "lsub { " );
             LineToText( pChild );
+            Append( "} " );
         }
         if( ( pChild = pSubSup->GetSubSup( RSUP ) ) ) {
             Separate( );
-            Append( "rsup " );
+            Append( "rsup { " );
             LineToText( pChild );
+            Append( "} " );
         }
         if( ( pChild = pSubSup->GetSubSup( RSUB ) ) ) {
             Separate( );
-            Append( "rsub " );
+            Append( "rsub { " );
             LineToText( pChild );
+            Append( "} " );
         }
         if( ( pChild = pSubSup->GetSubSup( CSUP ) ) ) {
             Separate( );
-            Append( "csup " );
+            Append( "csup { " );
             LineToText( pChild );
+            Append( "} " );
         }
         if( ( pChild = pSubSup->GetSubSup( CSUB ) ) ) {
             Separate( );
-            Append( "csub " );
+            Append( "csub { " );
             LineToText( pChild );
+            Append( "} " );
         }
     }
     LineToText( pNode->GetSubNode( 1 ) );
@@ -2401,19 +2407,23 @@ void SmNodeToTextVisitor::Visit( SmBinVerNode* pNode )
 {
     SmNode *pNum    = pNode->GetSubNode( 0 ),
            *pDenom  = pNode->GetSubNode( 2 );
+    Append( "{ " );
     LineToText( pNum );
     Append( "over" );
     LineToText( pDenom );
+    Append( "} " );
 }
 
 void SmNodeToTextVisitor::Visit( SmBinDiagonalNode* pNode )
 {
     SmNode *pLeftOperand  = pNode->GetSubNode( 0 ),
            *pRightOperand = pNode->GetSubNode( 1 );
+    Append( "{ " );
     LineToText( pLeftOperand );
     Separate( );
     Append( "wideslash " );
     LineToText( pRightOperand );
+    Append( "} " );
 }
 
 void SmNodeToTextVisitor::Visit( SmSubSupNode* pNode )
@@ -2525,7 +2535,7 @@ void SmNodeToTextVisitor::Visit( SmLineNode* pNode )
 
 void SmNodeToTextVisitor::Visit( SmExpressionNode* pNode )
 {
-    bool bracketsNeeded = pNode->GetNumSubNodes() != 1 || pNode->GetSubNode(0)->GetType() != 
NEXPRESSION;
+    bool bracketsNeeded = pNode->GetNumSubNodes() != 1 || pNode->GetSubNode(0)->GetType() == 
NBINHOR;
     if (bracketsNeeded) {
         Append( "{ " );
     }
-- 
1.7.3.5


Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.