Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.00 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <chrono>
  5. #include <ctime>
  6.  
  7. class natural {
  8. private:
  9.     std::vector<uint32_t> value;
  10.  
  11. public:
  12.     /* Konstruktor bezargumentowy */
  13.     natural::natural() {}
  14.     /* Konstruktor kopiujacy, z uinta */
  15.     natural::natural(const uint32_t a) {
  16.         this->value.push_back(a);
  17.     }
  18.     /* Konstruktor kopiujacy, z vectora */
  19.     natural::natural(const std::vector<uint32_t> &a) {
  20.         this->value = a;
  21.     }
  22.     /* Konstruktor kopiujacy, z natural */
  23.     natural::natural(const natural &a) : value(a.value) {}
  24.  
  25.     /* getter do wartosci */
  26.     std::vector<uint32_t> natural::getValue() const {
  27.         return this->value;
  28.     }
  29.     /* setter do wartosci */
  30.     void natural::setValue(const std::vector<uint32_t> &a) {
  31.         this->value = a;
  32.     }
  33.     /* Funkcja, ktora dodaje uinta32 do wartosci */
  34.     void natural::push_back(const uint32_t a) {
  35.         this->value.push_back(a);
  36.     }
  37.     /* Funkcja, ktora usuwa element o najwyzszej wadze */
  38.     void natural::pop_back() {
  39.         this->value.pop_back();
  40.     }
  41.     /* liczba elementow */
  42.     unsigned natural::size() const {
  43.         return this->value.size();
  44.     }
  45.     /* liczba bitow, potrzebnych do zapisania liczby */
  46.     unsigned natural::bitSize() const {
  47.         return 32 * this->value.size();
  48.     }
  49.     /* operator przypisania */
  50.     void natural::operator=(const natural &x) {
  51.         value = x.getValue();
  52.     }
  53.     /* operator porownania */
  54.     bool natural::operator==(const natural &x) const {
  55.         if (value.size() != x.value.size())
  56.             return false;
  57.         else {
  58.             // porownywanie kolejnych elementow
  59.             for (unsigned i = 0; i < x.value.size(); i++)
  60.                 if (value[i] != x.value[i])
  61.                     return false;
  62.             return true;
  63.         }
  64.     }
  65.     /* operator porownania do pojedynczej liczby */
  66.     bool natural::operator==(const unsigned &x) const {
  67.         if (value.size() > 1)
  68.             return false;
  69.         else {
  70.             if (value[0] == x)
  71.                 return true;
  72.             else
  73.                 return false;
  74.         }
  75.     }
  76.     /* operator inkrementacji */
  77.     void natural::operator++() {
  78.         unsigned i = 0;
  79.         bool cIn = true;
  80.         for (; i < this->value.size() && cIn; i++) {
  81.             this->value[i]++;
  82.             cIn = (this->value[i] == 0);
  83.         }
  84.         if (cIn) {
  85.             this->value.push_back(1);
  86.         }
  87.     }
  88.     /* operator dekrementacji */
  89.     void natural::operator--() {
  90.         if (this->isZero())
  91.             throw std::runtime_error("Dekrementacja 0 niedozwolona\n");
  92.         int i = 0;
  93.         bool bIn = true;
  94.         for (; bIn; i++) {
  95.             bIn = (this->value[i] == 0);
  96.             this->value[i]--;
  97.         }
  98.     }
  99.  
  100.     /* operator porownania, wiekszy */
  101.     bool natural::operator >(const natural &x) const {
  102.         if (this->value.size() > x.value.size())
  103.             return true;
  104.         else if (this->value.size() < x.value.size())
  105.             return false;
  106.         else { //argumenty tej samej dlugosci
  107.             // porownywanie kolejnych elementow od lewej (tych o wyzszej wadze)
  108.             for (int i = this->value.size() - 1; i >= 0; i--)
  109.                 if (this->value[i] > x.value[i])
  110.                     return true;
  111.             return false;
  112.         }
  113.     }
  114.     /* operator porownania, mniejszy */
  115.     bool natural::operator <(const natural &x) const {
  116.         if (this->value.size() < x.value.size())
  117.             return true;
  118.         else if (this->value.size() > x.value.size())
  119.             return false;
  120.         else { //argumenty tej samej dlugosci
  121.             // porownywanie kolejnych elementow od lewej (tych o wyzszej wadze)
  122.             for (int i = this->value.size() - 1; i >= 0; i--)
  123.                 if (this->value[i] < x.value[i])
  124.                     return true;
  125.             return false;
  126.         }
  127.     }
  128.     /* operator porownania, wiekszy/rowny */
  129.     bool natural::operator >=(const natural &x) const {
  130.         unsigned int counter = 0; //licznik rownych elementow
  131.         if (this->value.size() > x.value.size())
  132.             return true;
  133.         else if (this->value.size() < x.value.size())
  134.             return false;
  135.         else { //argumenty tej samej dlugosci
  136.             // sprawdzenie czy jest wiekszy
  137.             for (unsigned int i = this->value.size() - 1; i >= 0; i--) {
  138.                 if (this->value[i] > x.value[i])
  139.                     return true;
  140.                 else if (this->value[i] == x.value[i]) //sprawdzenie czy rowne
  141.                     counter++;
  142.             }
  143.             if (counter == this->value.size()) //wszystkie elementy sa rowne
  144.                 return true;
  145.             else
  146.                 return false;
  147.         }
  148.     }
  149.     /* operator porownania, mniejszy/rowny */
  150.     bool natural::operator <=(const natural &x) const {
  151.         unsigned int counter = 0; //licznik rownych elementow
  152.         if (this->value.size() < x.value.size())
  153.             return true;
  154.         else if (this->value.size() > x.value.size())
  155.             return false;
  156.         else { //argumenty tej samej dlugosci
  157.             // sprawdzenie czy jest mniejszy
  158.             for (unsigned int i = this->value.size() - 1; i >= 0; i--) {
  159.                 if (this->value[i] < x.value[i])
  160.                     return true;
  161.                 else if (this->value[i] == x.value[i]) //sprawdzenie czy rowne
  162.                     counter++;
  163.             }
  164.             if (counter == this->value.size()) //wszystkie elementy sa rowne
  165.                 return true;
  166.             else
  167.                 return false;
  168.         }
  169.     }
  170.  
  171.     /* wypisywanie wartosci */
  172.     void natural::print() const {
  173.         for (int i = value.size() - 1; i >= 0; i--)
  174.             std::cout << value[i] << " ";
  175.         std::cout << "\n";
  176.     }
  177.     /* usuwanie wiodacego zera */
  178.     void natural::eraseLeadingZeroIfExists() {
  179.         if ((this->value.size() - 1) == 0) //jesli elementem o najwyzszej wadze jest 0
  180.             this->value.pop_back(); //trzeba je usunac, zaburza wykonanie porownania
  181.     }
  182.  
  183.     bool natural::isZero() const {
  184.         if (this->value.size() == 0)
  185.             return true;
  186.         if (this->value.size() == 1 && value[0] == 0)
  187.             return true;
  188.         for (unsigned i = 0; i < this->value.size() - 1; i++) {
  189.             if (this->value[i] != 0)
  190.                 return false;
  191.         }
  192.         return true;
  193.     }
  194.     /* operacje arytmetyczne */
  195.     /* dodawanie */
  196.     void natural::add(const natural &x, const natural &y) {
  197.         bool cIn, cOut; //przeniesienia na konkretnych pozycjach
  198.         uint32_t temp; //przechowywanie sumy
  199.         unsigned i = 0; //indeksowanie
  200.  
  201.         const natural *shorterArg, *longerArg; //zmienne na krotszy i dluzszy argument
  202.         if (x.value.size() >= y.value.size()) { //rownej dlugosci lub x dluzszy od y
  203.             longerArg = &x;
  204.             shorterArg = &y;
  205.         }
  206.         else { //y dluzszy od x
  207.             longerArg = &y;
  208.             shorterArg = &x;
  209.         }
  210.         // dla kazdego elementu w zasiegu krotszego argumentu
  211.         for (cIn = false; i < shorterArg->value.size(); i++) {
  212.             temp = longerArg->value[i] + shorterArg->value[i];
  213.             cOut = (temp < longerArg->value[i]); //sprawdzenie czy uint sie "nie przekrecil"
  214.             if (cIn) {
  215.                 temp++;
  216.                 cOut |= (temp == 0); // cOut | temp
  217.             }
  218.             this->value.push_back(temp); // zapisanie wyniku
  219.             cIn = cOut; // przeniesienie w nastepnej iteracji
  220.         }
  221.         //w zasiegu dluzszego argumentu z przeniesieniem
  222.         for (; i < longerArg->value.size() && cIn; i++) {
  223.             temp = longerArg->value[i] + 1;
  224.             cIn = (temp == 0); //max val + 1 = 0
  225.             this->value.push_back(temp);
  226.         }
  227.         //w zasiegu dluzszego argumentu bez przeniesienia
  228.         for (; i < longerArg->value.size(); i++)
  229.             this->value.push_back(longerArg->value[i]); //przepisywanie
  230.         //Dodatkowy element w przypadku nadmiaru
  231.         if (cIn)
  232.             this->value.push_back(1);
  233.     }
  234.     /* odejmowanie */
  235.     void natural::subtract(const natural &x, const natural &y) {
  236.         bool bIn, bOut; //pozyczki na konkretnych pozycjach
  237.         uint32_t temp; //przechowywanie roznicy
  238.         unsigned i = 0; //indeksowanie
  239.  
  240.         for (bIn = false; i < y.value.size(); i++) {
  241.             temp = x.value[i] - y.value[i];
  242.             bOut = (temp > x.value[i]); //sprawdzenie czy uint sie "nie przekrecil"
  243.             if (bIn) {
  244.                 bOut |= (temp == 0); // bOut | temp
  245.                 temp--;
  246.             }
  247.             this->value.push_back(temp); // zapisanie wyniku
  248.             bIn = bOut; // pozyczka w nastepnej iteracji
  249.         }
  250.         // dalsze pozyczki
  251.         for (; i < x.value.size() && bIn; i++) {
  252.             bIn = (x.value[i] == 0);
  253.             this->value.push_back(x.value[i] - 1);
  254.         }
  255.         if (bIn) {
  256.             this->value.clear();
  257.             throw std::runtime_error("Wynik odejmowania ujemny");
  258.         }
  259.         else //dalsze przepisywanie odjemnej, bez zmian
  260.             for (; i < x.value.size(); i++)
  261.                 this->value.push_back(x.value[i]);
  262.     }
  263.  
  264.     void natural::multiply(const natural &x, const natural &y) {
  265.         int cIn = 0;
  266.         int cOut = 0;
  267.         uint32_t temp = 0;
  268.         std::vector<uint32_t> tempMod;
  269.         natural tempVec;
  270.         natural tempNat;
  271.         unsigned long long tempx = 0;
  272.         unsigned long long tempy = 0;
  273.  
  274.  
  275.         unsigned long long longtemp;
  276.         for (unsigned i = 0; i < x.value.size(); i++) {
  277.             for (unsigned k = 0; k < i; k++) {
  278.                 tempVec.value.insert(tempVec.value.begin(), 0);
  279.             }
  280.             for (unsigned j = 0; j < y.value.size(); j++) {
  281.                 tempx = x.value[i];
  282.                 tempy = y.value[j];
  283.                 longtemp = tempx * tempy + cIn;
  284.                 temp = longtemp;
  285.                 tempVec.value.insert(tempVec.value.begin() + i + j, temp);
  286.                 if (temp == longtemp) {
  287.                     cIn = 0;
  288.                 }
  289.                 else {
  290.                     cIn = longtemp / 4294967295;
  291.                 }
  292.                 temp = 0;
  293.  
  294.             }
  295.             tempMod = this->value;
  296.             tempNat.value = tempMod;
  297.             this->value.clear();
  298.             this->add(tempNat, tempVec);
  299.             tempVec.value.clear();
  300.             tempMod.clear();
  301.         }
  302.         if (cIn != 0)
  303.             this->value.push_back(cIn);
  304.     }
  305.  
  306.  
  307.     // prymitywny algorytm dzielenia
  308.     void natural::divide(const natural &x, const natural &y) {
  309.         if ((y.value.empty()) | (y.value[0] == 0))
  310.             throw std::runtime_error("Dzielenie przez 0\n");
  311.         natural divident(x); //kopia do dzialan dzielnej
  312.         natural quotient(0); //przechowywanie ilorazu, quotient
  313.         natural temp;
  314.  
  315.         while (divident.operator>(y)) { //powinno byc >= ale samo > to mniej operacji
  316.             temp.subtract(divident, y);
  317.             divident.value = temp.value; //kopiowanie wartosci
  318.             //divident.print();
  319.             temp.value.clear(); //zwalnianie pamieci z temp
  320.             quotient.operator++();
  321.             divident.eraseLeadingZeroIfExists();
  322.         }
  323.         if (divident.operator==(y)) { //ostatnie porownanie
  324.             temp.subtract(divident, y);
  325.             divident.value = temp.value; //kopiowanie wartosci
  326.             temp.value.clear(); //zwalnianie pamieci z temp
  327.             quotient.operator++();
  328.         }
  329.         this->value = quotient.value;
  330.         temp.value.clear(); //zwalnianie pamieci z temp
  331.         divident.value.clear();
  332.     }
  333. };
  334.  
  335. class smNum
  336. {
  337.     bool sign; //false +, true -
  338.     natural module; //modul reprezentowany przez liczbe naturalna
  339. public:
  340.     /* Konstruktor bezargumentowy */
  341.     smNum::smNum()
  342.         : sign(),
  343.         module() { }
  344.     /* Konstruktor kopiujacy, z uinta */
  345.     smNum::smNum(const uint32_t a) {
  346.         sign = false;
  347.         this->module.push_back(a);
  348.     }
  349.     /* Konstruktor sam modul */
  350.     smNum::smNum(const std::vector<uint32_t> &a) {
  351.         sign = false;
  352.         module = natural(a);
  353.     }
  354.  
  355.     /* Konstruktor znak-modul */
  356.     smNum::smNum(bool si, const std::vector<uint32_t> &a) {
  357.         sign = si;
  358.         module = natural(a);
  359.     }
  360.  
  361.     /* Konstruktor kopiujacy z smNum */
  362.     smNum::smNum(const smNum &b)
  363.         : sign(b.sign),
  364.         module(b.module) { }
  365.  
  366.     /* Konstruktor kopiujacy znak + natural */
  367.     smNum::smNum(const bool si, const natural &n)
  368.         : sign(si),
  369.         module(n) { }
  370.  
  371.     /* Konstruktor kopiujacy z natural */
  372.     smNum::smNum(const natural &n)
  373.         : sign(false),
  374.         module(n) { }
  375.  
  376.     /* Konstruktor z konwersja z typu long long */
  377.     smNum::smNum(long long value)
  378.     {
  379.         if (value < 0) {
  380.             sign = true;
  381.             module = natural(0);
  382.         }
  383.         else {
  384.             sign = false;
  385.         }
  386.  
  387.         while (value) {
  388.             module.push_back((uint32_t)(value));
  389.         }
  390.     }
  391.  
  392.     /* getter do znaku */
  393.     bool smNum::getSign() {
  394.         return this->sign;
  395.     }
  396.     /* setter do znaku */
  397.     void smNum::setSign(const bool si) {
  398.         this->sign = si;
  399.     }
  400.     /* getter do wartosci */
  401.     std::vector<uint32_t> smNum::getModule() {
  402.         return this->module.getValue();
  403.     }
  404.     /* getter do wartosci */
  405.     natural smNum::getNatural() {
  406.         return this->module;
  407.     }
  408.     /* setter do wartosci */
  409.     void smNum::setModule(const std::vector<uint32_t> &a) {
  410.         this->module.setValue(a);
  411.     }
  412.     /* setter do wartosci */
  413.     void smNum::setNatural(const natural &a) {
  414.         this->module = a;
  415.     }
  416.  
  417.     /* wypisywanie wartosci */
  418.     void smNum::print() {
  419.         if (sign)
  420.             std::cout << "- ";
  421.         else
  422.             std::cout << "+ ";
  423.  
  424.         module.print();
  425.     }
  426.  
  427.     /* operator przypisania */
  428.     void smNum::operator=(const smNum &x) {
  429.         this->sign = x.sign;
  430.         this->module = x.module;
  431.     }
  432.     /* operator porownania */
  433.     bool smNum::operator==(const smNum &x) {
  434.         if (this->sign != x.sign) //jesli znaki sa rozne false
  435.             return false;
  436.         else //porownanie wartosci
  437.             if (this->module == x.module)
  438.                 return true;
  439.             else
  440.                 return false;
  441.     }
  442.     // /* operator porownania do liczby*/
  443.     // bool smNum::operator==(const int &x) {
  444.     //  if(this->module.size() > 1)
  445.     //      return false;
  446.     //  if(x > 0 || this->sign == false){
  447.     //      if(this->module[0]== x)
  448.     //          return true;
  449.     //      else
  450.     //          return false;
  451.     //  }
  452.     //  if(x < 0 || this->sign == true){
  453.     //      if(this->module[0] == x)
  454.     //          return true;
  455.     //      else
  456.     //          return false;
  457.     //  }
  458.     // }
  459.     /* operator inkrementacji */
  460.     void smNum::operator++() {
  461.         if (!sign) //dodatnie
  462.             this->module.operator++();
  463.         else //ujemne
  464.             this->module.operator--();
  465.     }
  466.     /* operator dekrementacji */
  467.     void smNum::operator--() {
  468.         if (!sign) //dodatnie
  469.             this->module.operator--();
  470.         else //ujemne
  471.             this->module.operator++();
  472.     }
  473.  
  474.     /* operator porownania, wiekszy */
  475.     bool smNum::operator >(const smNum &x) {
  476.         if (this->sign != x.sign) { //znaki rozne
  477.             if (this->sign == 0 && x.sign == 1) //x jest liczba ujemna
  478.                 return true;
  479.             else
  480.                 return false; //x dodatnie a liczba ujemna
  481.         }
  482.         else { //znaki zgodne
  483.             if (this->sign == 0) { //obie liczby dodatnie
  484.                 if (this->module > x.module)
  485.                     return true;
  486.                 else
  487.                     return false;
  488.             }
  489.             else { //obie liczby ujemne
  490.                 if (this->module < x.module)
  491.                     return true;
  492.                 else
  493.                     return false;
  494.             }
  495.         }
  496.     }
  497.     /* operator porownania, mniejszy */
  498.     bool smNum::operator <(const smNum &x) {
  499.         if (this->sign != x.sign) { //znaki rozne
  500.             if (this->sign == 0 && x.sign == 1) //x jest liczba ujemna
  501.                 return false;
  502.             else
  503.                 return true; //x dodatnie a liczba ujemna
  504.         }
  505.         else { //znaki zgodne
  506.             if (this->sign == 0) { //obie liczby dodatnie
  507.                 if (this->module < x.module)
  508.                     return true;
  509.                 else
  510.                     return false;
  511.             }
  512.             else { //obie liczby ujemne
  513.                 if (this->module > x.module)
  514.                     return true;
  515.                 else
  516.                     return false;
  517.             }
  518.         }
  519.     }
  520.     /* operator porownania, wiekszy/rowny */
  521.     bool smNum::operator >=(const smNum &x) {
  522.         if (this->sign != x.sign) { //znaki rozne
  523.             if (this->sign == 0 && x.sign == 1) //x jest liczba ujemna
  524.                 return true;
  525.             else
  526.                 return false; //x dodatnie a liczba ujemna
  527.         }
  528.         else { //znaki zgodne
  529.             if (this->sign == 0) { //obie liczby dodatnie
  530.                 if (this->module >= x.module)
  531.                     return true;
  532.                 else
  533.                     return false;
  534.             }
  535.             else { //obie liczby ujemne
  536.                 if (this->module <= x.module)
  537.                     return true;
  538.                 else
  539.                     return false;
  540.             }
  541.         }
  542.     }
  543.     /* operator porownania, mniejszy/rowny */
  544.     bool smNum::operator <=(const smNum &x) {
  545.         if (this->sign != x.sign) { //znaki rozne
  546.             if (this->sign == 0 && x.sign == 1) //x jest liczba ujemna
  547.                 return false;
  548.             else
  549.                 return true; //x dodatnie a liczba ujemna
  550.         }
  551.         else { //znaki zgodne
  552.             if (this->sign == 0) { //obie liczby dodatnie
  553.                 if (this->module <= x.module)
  554.                     return true;
  555.                 else
  556.                     return false;
  557.             }
  558.             else { //obie liczby ujemne
  559.                 if (this->module >= x.module)
  560.                     return true;
  561.                 else
  562.                     return false;
  563.             }
  564.         }
  565.     }
  566.     smNum smNum::operator+(smNum x) {
  567.         smNum result;
  568.         smNum arg(this->sign, this->module);
  569.         result.add(arg, x);
  570.         return result;
  571.     }
  572.     smNum smNum::operator-(smNum x) {
  573.         smNum result;
  574.         smNum arg(this->sign, this->module);
  575.         result.sub(arg, x);
  576.         return result;
  577.     }
  578.     smNum smNum::operator*(smNum x) {
  579.         smNum result;
  580.         smNum arg(this->sign, this->module);
  581.         result.mul(arg, x);
  582.         return result;
  583.     }
  584.     smNum smNum::operator/(smNum x) {
  585.         smNum result;
  586.         smNum arg(this->sign, this->module);
  587.         result.div(arg, x);
  588.         return result;
  589.     }
  590.     /* operacje arytmetyczne */
  591.     // dodawanie
  592.     void smNum::add(smNum x, smNum y) {
  593.         if (x.sign == y.sign) { //znaki zgodne
  594.             this->sign = x.sign;
  595.             this->module.add(x.module, y.module);
  596.         }
  597.         else { //znaki niezgodne
  598.             if (x.module >= y.module) {
  599.                 this->sign = x.sign;
  600.                 this->module.subtract(x.module, y.module);
  601.             }
  602.             else
  603.             {
  604.                 this->sign = y.sign;
  605.                 this->module.subtract(y.module, x.module);
  606.             }
  607.         }
  608.         //this->module.eraseLeadingZeroIfExists();
  609.     }
  610.     // odejmowanie
  611.     void smNum::sub(smNum x, smNum y) {
  612.         if (x.sign == y.sign) { //znaki zgodne
  613.             if (x.module >= y.module) {
  614.                 this->sign = x.sign;
  615.                 this->module.subtract(x.module, y.module);
  616.             }
  617.             else
  618.             {
  619.                 this->sign = !(x.sign);
  620.                 this->module.subtract(y.module, x.module);
  621.             }
  622.         }
  623.         else { //znaki niezgodne
  624.             this->sign = x.sign;
  625.             if (x.sign == 0) {
  626.                 this->module.add(x.module, y.module);
  627.             }
  628.             else
  629.             {
  630.                 this->module.add(x.module, y.module);
  631.             }
  632.         }
  633.     }
  634.     // mnozenie
  635.     void smNum::mul(smNum x, smNum y) {
  636.         if (x.sign == y.sign)
  637.             this->setSign(0);
  638.         else
  639.             this->setSign(1);
  640.  
  641.         this->module.multiply(x.module, y.module);
  642.     }
  643.     // prymitywny algorytm dzielenia
  644.     void smNum::div(smNum x, smNum y) {
  645.         if (x.sign == y.sign)
  646.             this->sign = 0;
  647.         else
  648.             this->sign = 1;
  649.  
  650.         this->module.divide(x.module, y.module);
  651.     }
  652. };
  653.  
  654. /*bool equals(const smNum &x, const smNum &y) {
  655.  
  656.     if (x.getSign(x) != y.getSign(y)) //jesli znaki sa rozne false
  657.         return false;
  658.     else //porownanie wartosci
  659.         if (x.getModule(x) == y.getModule(y))
  660.             return true;
  661.         else
  662.             return false;
  663. }*/
  664. //Testy operacji arytmetycznych na Natural
  665. TEST(NaturalAddTest, natAdd1) {
  666.     auto a = natural({ 17,22,11 });
  667.     auto b = natural({ 12,11,10 });
  668.     auto c = natural({ 29,33,21 });
  669.     natural wynik;
  670.     wynik.add(a, b);
  671.     EXPECT_EQ(wynik.getValue(), c.getValue());
  672.  
  673. };
  674.  
  675. TEST(NaturalSubTest, natSub1) {
  676.     auto a = natural({ 17,22,11 });
  677.     auto b = natural({ 12,11,10 });
  678.     auto c = natural({ 5,11,1 });
  679.     natural wynik;
  680.     wynik.subtract(a, b);
  681.     EXPECT_EQ(wynik.getValue(), c.getValue());
  682.  
  683. };
  684. TEST(NaturalDivTest, natDiv1) {
  685.     auto a = natural({ 17,22,11 });
  686.     auto b = natural({ 12,11,10 });
  687.     auto c = natural({ 1 });
  688.     natural wynik;
  689.     wynik.divide(a, b);
  690.     EXPECT_EQ(wynik.getValue(), c.getValue());
  691.  
  692. };
  693.  
  694. //Testy operacji arytmetycznych na smNum wykorzystujac Natural
  695.  
  696. //dodawanie znaki +
  697. TEST(smNumAddTest, smNumAdd1) {
  698.     auto a = natural({ 82931, 2136512, 12376 });
  699.     auto b = natural({ 192,62 });
  700.     auto c = natural({ 83123, 2136574, 12376 });
  701.     auto  a1 = smNum( 0,a );
  702.     auto  b1 = smNum( 0,b );
  703.     auto c1 = smNum( 0,c );
  704.     smNum wynik;
  705.     wynik.add(a1, b1);
  706.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  707.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  708. }
  709. //dodawanie znaki -
  710. TEST(smNumAddTest, smNumAdd2) {
  711.     auto a = natural({ 27623,9152, 9124 });
  712.     auto b = natural({  1192, 1925});
  713.     auto c = natural({ 28815, 11077, 9124});
  714.     auto  a1 = smNum({ 1,a });
  715.     auto  b1 = smNum({ 1,b });
  716.     auto c1 = smNum({ 1,c });
  717.     smNum wynik;
  718.     wynik.add(a1, b1);
  719.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  720.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  721. }
  722.  
  723. //dodawanie roznych znakow
  724. TEST(smNumAddTest, smNumAdd3) {
  725.     auto a = natural({ 21367134, 12487124, 12387128 });
  726.     auto b = natural({ 1192, 39713847 });
  727.     auto c = natural({   21365942, 4267740573, 12387127 });
  728.     auto  a1 = smNum({ 0,a });
  729.     auto  b1 = smNum({ 1,b });
  730.     auto c1 = smNum({ 0,c });
  731.     smNum wynik;
  732.     wynik.add(a1, b1);
  733.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  734.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  735. }
  736.  
  737. TEST(smNumAddTest, smNumAdd4) {
  738.     auto a = natural({ 1233453,120391,9471393 });
  739.     auto b = natural({ 1192, 1925123 });
  740.     auto c = natural({ 1232261, 4293162564, 9471392 });
  741.     auto  a1 = smNum({ 1,a });
  742.     auto  b1 = smNum({ 0,b });
  743.     auto c1 = smNum({ 1,c });
  744.     smNum wynik;
  745.     wynik.add(a1, b1);
  746.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  747.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  748. }
  749.  
  750.  
  751. //odejmowanie, znaki +
  752. TEST(smNumSubTest, smNumSub1) {
  753.     auto a = natural({ 27623,9152, 9124 });
  754.     auto b = natural({ 1192, 1925 });
  755.     auto c = natural({ 26431, 7227, 9124});
  756.     auto  a1 = smNum({ 0,a });
  757.     auto  b1 = smNum({ 0,b });
  758.     auto c1 = smNum({ 0,c });
  759.     smNum wynik;
  760.     wynik.sub(a1, b1);
  761.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  762.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  763. }
  764. // znaki -
  765. TEST(smNumSubTest, smNumSub2) {
  766.     auto a = natural({ 92837, 11825, 8291028 });
  767.     auto b = natural({ 1209327, 1238712, 423184, 81236 });
  768.     auto c = natural({ 1116490, 1226887, 4287099452, 81235 });
  769.     auto  a1 = smNum({ 1,a });
  770.     auto  b1 = smNum({ 1,b });
  771.     auto c1 = smNum({ 0,c });
  772.     smNum wynik;
  773.     wynik.sub(a1, b1);
  774.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  775.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  776. }
  777. //rozne znaki
  778. TEST(smNumSubTest, smNumSub3) {
  779.     auto a = natural({ 152637, 12872136, 935, 1 });
  780.     auto b = natural({ 17829, 8263, 901238 });
  781.     auto c = natural({ 170466, 12880399, 902173, 1});
  782.     auto  a1 = smNum({ 1,a });
  783.     auto  b1 = smNum({ 0,b });
  784.     auto c1 = smNum({ 1,c });
  785.     smNum wynik;
  786.     wynik.sub(a1, b1);
  787.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  788.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  789. }
  790.  
  791. TEST(smNumSubTest, smNumSub4) {
  792.     auto a = natural({ 12678, 9351, 82643, 826});
  793.     auto b = natural({ 56373, 8299, 9, 1 });
  794.     auto c = natural({ 69051, 17650, 82652, 827 });
  795.     auto  a1 = smNum({ 0,a });
  796.     auto  b1 = smNum({ 1,b });
  797.     auto c1 = smNum({ 0,c });
  798.     smNum wynik;
  799.     wynik.sub(a1, b1);
  800.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  801.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  802. }
  803.  
  804. //testy mnozenia, znaki +
  805.  
  806. TEST(smNumMulTest, smNumMul1) {
  807.     auto a = natural({ 27623,9152, 9124 });
  808.     auto b = natural({ 1192, 1925 });
  809.     auto c = natural({ 32926616,64083459, 28493408, 17563700 });
  810.     auto  a1 = smNum({ 0,a });
  811.     auto  b1 = smNum({ 0,b });
  812.     auto c1 = smNum({ 0,c });
  813.     smNum wynik;
  814.     wynik.mul(a1, b1);
  815.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  816.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  817. }
  818.  
  819. //znaki -
  820. TEST(smNumMulTest, smNumMul2) {
  821.     auto a = natural({ 36192, 129 });
  822.     auto b = natural({ 93, 912647, 92137, 9123 });
  823.     auto c = natural({ 3365856, 2965761149, 3452353774, 342065289, 1176867 });
  824.     auto  a1 = smNum({ 1,a });
  825.     auto  b1 = smNum({ 1,b });
  826.     auto c1 = smNum({ 0,c });
  827.     smNum wynik;
  828.     wynik.mul(a1, b1);
  829.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  830.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  831. }
  832. //rozne znaki
  833.  
  834. TEST(smNumMulTest, smNumMul3) {
  835.     auto a = natural({ 217, 81, 9213});
  836.     auto b = natural({ 9 ,71 });
  837.     auto c = natural({ 1953, 16136, 88668, 654123 });
  838.     auto  a1 = smNum({ 1,a });
  839.     auto  b1 = smNum({ 0,b });
  840.     auto c1 = smNum({ 1,c });
  841.     smNum wynik;
  842.     wynik.mul(a1, b1);
  843.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  844.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  845. }
  846. TEST(smNumMulTest, smNumMul4) {
  847.     auto a = natural({ 2, 1 });
  848.     auto b = natural({ 4000000000, 1 });
  849.     auto c = natural({3705032704 , 4000000003, 1 });
  850.     auto  a1 = smNum({ 0,a });
  851.     auto  b1 = smNum({ 1,b });
  852.     auto c1 = smNum({ 1,c });
  853.     smNum wynik;
  854.     wynik.mul(a1, b1);
  855.     EXPECT_EQ(wynik.getModule(), c1.getModule());
  856.     EXPECT_EQ(wynik.getSign(), c1.getSign());
  857. }
  858. //badanie przeniesienia
  859. TEST(Przeniesienie, przen) {
  860.     auto a = natural({ 4294967293 });
  861.     auto b = natural({ 2  });
  862.     auto c = natural({ 4294967290 , 1});
  863.     natural wynik;
  864.     wynik.multiply(a, b);
  865.     EXPECT_EQ(c.getValue(), wynik.getValue());
  866. }
  867. //testy dzielenia
  868. TEST(smNumDivTest, smNumDiv1) {
  869.     auto a = natural({ 520 });
  870.     auto b = natural({ 511 });
  871.     auto c = natural({ 1 });
  872. //  auto a1 = smNum({ 0,a });
  873. //  auto b1 = smNum({ 0 , b });
  874. //  auto c1 = smNum({ 0, c });
  875.     natural wynik;
  876. //  wynik.div(a1, b1);
  877.     wynik.divide(a, b);
  878.     EXPECT_EQ(wynik.getValue() , c.getValue());
  879. //  EXPECT_EQ(wynik.getSign(), c1.getSign());
  880.  
  881. }
  882.  
  883.  
  884. TEST(TestOdejmowania, Odejmowanie) {
  885.     std::vector<uint32_t> vect;
  886.     std::vector<uint32_t> vect2;
  887.     vect.push_back(55);
  888.     vect.push_back(1);
  889.     vect2.push_back(16);
  890.     vect2.push_back(1);
  891.     std::vector<uint32_t> vect3;
  892.     vect3.push_back(39);
  893.     vect3.push_back(0);
  894.     smNum wynik(vect3);
  895.     smNum a1(vect);
  896.     smNum a2(vect2);
  897.     //wynik.sub(a1, a2);
  898.     smNum a3;
  899.     a3.sub(a1, a2);
  900.     a3.print();
  901.     wynik.print();
  902.      EXPECT_EQ(a3.getModule(),wynik.getModule());
  903.         EXPECT_EQ(a3.getSign(), wynik.getSign());
  904. }
  905.  
  906. TEST(NaturalTest, initWithZero) {
  907.     auto a = natural(0);
  908.     EXPECT_TRUE(a.isZero());
  909.     std::vector<uint32_t> zero;
  910.     zero.push_back(0);
  911.     EXPECT_EQ(a.getValue(), zero);
  912. }
  913.  
  914. TEST(NaturalTest, initWithVector) {
  915.     auto a = natural({ 1,2,3,4 });
  916.     EXPECT_EQ(a.size(), 4);
  917. }
  918. TEST(NaturalTest, initWithNatural) {
  919.     auto a = natural({ 6,5,4,3 });
  920.     natural b = natural(a);
  921.     EXPECT_TRUE(a == b); //test operatora ==
  922. }
  923.  
  924. TEST(NaturalTest, decremetZero) {
  925.     std::vector<uint32_t> zero;
  926.     zero.push_back(0);
  927.     auto a = natural(zero);
  928.     EXPECT_THROW(--a, std::runtime_error);
  929. }
  930. TEST(NaturalTest, divideByZero) {
  931.     auto a = natural(0);
  932.     auto b = natural({ 1266,16,7 });
  933.     natural c;
  934.     EXPECT_THROW(c.divide(b, a), std::runtime_error);
  935. }
  936. TEST(NaturalTest, resultIsLessThanZero) {
  937.     auto a = natural({ 2894,1 });
  938.     auto b = natural({ 1666,2 });
  939.     natural c;
  940.     EXPECT_THROW(c.subtract(a, b), std::runtime_error);
  941. }
  942.  
  943.  
  944. TEST(SingMagnitudeTest, emptyNumber2) {
  945.     unsigned val = 14;
  946.     std::vector<uint32_t> check;
  947.     auto a = smNum(1, val);
  948.     check.push_back(val);
  949.     EXPECT_TRUE(a.getSign());
  950.     EXPECT_EQ(a.getModule(), check);
  951. }
  952.  
  953. TEST(SingMagnitudeTest, emptyNumber3) {
  954.     unsigned val = 500;
  955.     auto a = natural(val); //natural o wartosci +500
  956.     std::vector<uint32_t> check;
  957.     auto b = smNum(0, a);
  958.     check.push_back(val);
  959.     EXPECT_FALSE(b.getSign());
  960.     EXPECT_EQ(b.getModule(), check);
  961. }
  962. int main(int argc, char* argv[]) {
  963.     ::testing::InitGoogleTest(&argc, argv);
  964.     return RUN_ALL_TESTS();
  965. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement