Advertisement
Guest User

Untitled

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