tests.cc 4.33 KB
Newer Older
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
/* Common header file for the rrspace software

This file is part of the rrspace project.

This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#include <iostream>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/extensions/HelperMacros.h>
#include "curve.h"
#include "divisor.h"
#include "divisor_class_group.h"
#include <sstream>

class EllipticCurveTest : public CppUnit::TestFixture { 
 private:
   BivPol *q;
   Curve *C;

 public: 
  void setUp() {
    NTL::ZZ_p::init(NTL::ZZ(1009));
    NTL::ZZ_pX q0, q1, q2;
    // Elliptic curve
    std::stringstream("[304 38 866 317]") >> q0;
    std::stringstream("[483 91 912]") >> q1;
    std::stringstream("[262 839]") >> q2;
    q = new BivPol({q0, q1, q2, NTL::ZZ_pX(1)});
    C = new Curve(q);
  }

  void tearDown() {
    delete C;
    delete q;
  }

  void testGroupLaw() {
53
    NTL::ZZ_pX fO, gO, fE, gE, f1, g1, f2, g2;
54 55 56 57 58 59 60 61
    
    // O = (781 : 643 : 1)
    std::stringstream("[228 1]") >> fO;
    std::stringstream("[643]") >> gO;
    Divisor O(
        EffectiveDivisor(*C, fO, gO),
        EffectiveDivisor(*C));

62 63 64 65 66
    // E is the zero divisor
    std::stringstream("[1]") >> fE;
    std::stringstream("[]") >> gE;
    EffectiveDivisor E(*C, fE, gE);

67 68 69 70 71 72 73 74 75 76 77
    // P1 = (381 : 777 : 1)
    std::stringstream("[628 1]") >> f1;
    std::stringstream("[777]") >> g1;
    EffectiveDivisor P1(*C, f1, g1);

    // P2 = (769 : 205 : 1)
    std::stringstream("[240 1]") >> f2;
    std::stringstream("[205]") >> g2;
    EffectiveDivisor P2(*C, f2, g2);

    EffectiveDivisor P3 =
78
      AddReduced(1, O, E, P1, P2);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    
    NTL::ZZ_pX fR, gR;

    // PR = (933 : 189 : 1)
    std::stringstream("[76 1]") >> fR;
    std::stringstream("[189]") >> gR;

    CPPUNIT_ASSERT(P3.get_f() == fR);
    CPPUNIT_ASSERT(P3.get_g() == gR);
  }
};

class SmoothQuarticTest : public CppUnit::TestFixture { 
 private:
   BivPol *q;
   Curve *C;

 public: 
  void setUp() {
    NTL::ZZ_p::init(NTL::ZZ(1009));
    NTL::ZZ_pX q0, q1, q2, q3;
    // Elliptic curve
    std::stringstream("[810 944 972 589 71]") >> q0;
    std::stringstream("[790 206 709 905]") >> q1;
    std::stringstream("[236 969 139]") >> q2;
    std::stringstream("[311 861]") >> q3;
    q = new BivPol({q0, q1, q2, q3, NTL::ZZ_pX(1)});
    C = new Curve(q);
  }

  void tearDown() {
    delete C;
    delete q;
  }

  void testGroupLaw() {
115
    NTL::ZZ_pX fO, gO, fE, gE, f1, g1, f2, g2;
116 117 118 119 120 121 122 123
    
    // O = (502 : 832 : 1)
    std::stringstream("[507 1]") >> fO;
    std::stringstream("[832]") >> gO;
    Divisor O(
        EffectiveDivisor(*C, fO, gO),
        EffectiveDivisor(*C));

124 125 126 127 128
    // E is the zero divisor
    std::stringstream("[1]") >> fE;
    std::stringstream("[]") >> gE;
    EffectiveDivisor E(*C, fE, gE);

129 130 131 132 133 134 135 136 137 138 139
    // D1
    std::stringstream("[374 74 387 1]") >> f1;
    std::stringstream("[78 130 82]") >> g1;
    EffectiveDivisor D1(*C, f1, g1);

    // D2
    std::stringstream("[752 293 10 1]") >> f2;
    std::stringstream("[17 109 43]") >> g2;
    EffectiveDivisor D2(*C, f2, g2);

    EffectiveDivisor D3 =
140
      AddReduced(3, O, E, D1, D2);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    
    NTL::ZZ_pX fR, gR;

    // PR
    std::stringstream("[512 804 472 1]") >> fR;
    std::stringstream("[405 263 475]") >> gR;

    CPPUNIT_ASSERT(D3.get_f() == fR);
    CPPUNIT_ASSERT(D3.get_g() == gR);
  }
};

int main() {
  CppUnit::TextUi::TestRunner runner;  
  CppUnit::TestCaller<EllipticCurveTest>* test1 = 
    new CppUnit::TestCaller<EllipticCurveTest>(
      "testRiemannRoch", 
      &EllipticCurveTest::testGroupLaw);
  CppUnit::TestCaller<SmoothQuarticTest>* test2 = 
    new CppUnit::TestCaller<SmoothQuarticTest>(
      "testRiemannRoch", 
      &SmoothQuarticTest::testGroupLaw);
  runner.addTest(test1);
  runner.addTest(test2);
  runner.run();
  return 0;
}