Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matanb1238 committed Apr 21, 2021
1 parent 26eae8b commit cf4ead6
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 48 deletions.
156 changes: 127 additions & 29 deletions NumberWithUnits.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,136 @@

#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <map>
#include <string.h>
#include "NumberWithUnits.hpp"
// #define BOOL true
// #define BOOL true

using namespace std;
//m -> cm
//cm -> m
//km -> m
//m -> km
namespace ariel
{
static map<string, map<string, double>> convertor; // convertor O(3)

// using namespace std;
double convertHelp(const string &from, const string &to, double fromVal)
{
if (from == to)
{
return fromVal;
}
if (convertor.at(from).count(to) != 0)
{
return fromVal * convertor.at(from).at(to);
}
__throw_invalid_argument("Not valid convertion");

}

namespace ariel{
void NumberWithUnits::read_units(const ifstream& file){}
NumberWithUnits operator+(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return NumberWithUnits();}
NumberWithUnits operator+=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return NumberWithUnits();}
NumberWithUnits operator+(const NumberWithUnits& unit){return NumberWithUnits();}
NumberWithUnits operator-(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return NumberWithUnits();}
NumberWithUnits operator-=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return NumberWithUnits();}
NumberWithUnits operator-(const NumberWithUnits& unit){return NumberWithUnits();}
bool operator>(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;}
bool operator>=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;}
bool operator<(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;}
bool operator<=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;}
bool operator==(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;}
bool operator!=(const NumberWithUnits& unit1, const NumberWithUnits& unit2){return unit1.BOOL;}
NumberWithUnits operator++(const NumberWithUnits& unit){return NumberWithUnits();} //Prefix
NumberWithUnits operator++(const NumberWithUnits& unit, int){return NumberWithUnits();} //Postfix
NumberWithUnits operator--(const NumberWithUnits& unit){return NumberWithUnits();} //Prefix
NumberWithUnits operator--(const NumberWithUnits& unit, int){return NumberWithUnits();} //Postfix
NumberWithUnits operator*(const NumberWithUnits& unit, double num){return NumberWithUnits();}
NumberWithUnits operator*(double num, const NumberWithUnits& unit){return NumberWithUnits();}
ostream& operator<<(const ostream& os, const NumberWithUnits& unit){return cout << unit._value << "[" << unit._unit << "]" << endl;}
istream& operator>>(std::istream& in, NumberWithUnits& unit){
string s;
in >> unit._value >> s >> unit._unit;
return in;
double convert(const string &from, const string &to, double fromVal)
{
if (from == to)
{
return fromVal;
}
if (convertor.at(from).count(to) != 0)
{
return fromVal * convertor.at(from).at(to);
}
map<string, double> m = convertor.at(from);
string s;
for (map<string, double>::iterator it = m.begin(); it != m.end(); ++it)
{
s = it->first;
}
return convertHelp(s, to, convert(from, s, fromVal));
}

void NumberWithUnits::read_units(ifstream &file)
{
double Dunit1 = 0;
double Dunit2 = 0;
string Munit1;
string Munit2;
string none;
string line;
while (getline(file, line))
{
istringstream Sstream(line);
if (!(Sstream >> Dunit1 >> Munit1 >> none >> Dunit2 >> Munit2))
{
break;
}
}

convertor[Munit1][Munit2] = Dunit2; //from
convertor[Munit2][Munit1] = 1 / Dunit2; //to
}
}

NumberWithUnits operator+(const NumberWithUnits &unit1, const NumberWithUnits &unit2)
{
double converted = convert(unit2._unit, unit1._unit, unit2._value);
return NumberWithUnits(unit1._value + converted, unit1._unit);
}
NumberWithUnits operator+=(NumberWithUnits &unit1, const NumberWithUnits &unit2)
{
unit1._value += convert(unit2._unit, unit1._unit, unit2._value);
return unit1;
}
NumberWithUnits operator+(const NumberWithUnits &unit, double num)
{
return NumberWithUnits(unit._value + num, unit._unit);
}
NumberWithUnits operator-(const NumberWithUnits &unit1, const NumberWithUnits &unit2)
{
double converted = convert(unit2._unit, unit1._unit, unit2._value);
return NumberWithUnits(unit1._value - converted, unit1._unit);
}
NumberWithUnits operator-=(NumberWithUnits &unit1, const NumberWithUnits &unit2)
{
unit1._value -= convert(unit2._unit, unit1._unit, unit2._value);
return unit1;
}
NumberWithUnits operator-(const NumberWithUnits &unit)
{
return NumberWithUnits(-unit._value, unit._unit);
}
int compare(const NumberWithUnits &n1, const NumberWithUnits &n2)
{
double x = n1._value - convert(n2._unit, n1._unit, n2._value);
double y = convert(n2._unit, n1._unit, n2._value) - n1._value;
const double epsilon = 0.00001;
if (x > epsilon)
{
return 1;
}
if (y > epsilon)
{
return -1;
}
return 0;
}

bool operator>(const NumberWithUnits &unit1, const NumberWithUnits &unit2) { return compare(unit1, unit2) > 0; }
bool operator>=(const NumberWithUnits &unit1, const NumberWithUnits &unit2) { return compare(unit1, unit2) >= 0; }
bool operator<(const NumberWithUnits &unit1, const NumberWithUnits &unit2) { return compare(unit1, unit2) < 0; }
bool operator<=(const NumberWithUnits &unit1, const NumberWithUnits &unit2) { return compare(unit1, unit2) <= 0; }
bool operator==(const NumberWithUnits &unit1, const NumberWithUnits &unit2) { return compare(unit1, unit2) == 0; }
bool operator!=(const NumberWithUnits &unit1, const NumberWithUnits &unit2) { return compare(unit1, unit2) != 0; }
NumberWithUnits operator++(NumberWithUnits &unit) { return NumberWithUnits(++unit._value, unit._unit); } //Prefix
NumberWithUnits operator++(NumberWithUnits &unit, int) { return NumberWithUnits(unit._value++, unit._unit); } //Postfix
NumberWithUnits operator--(NumberWithUnits &unit) { return NumberWithUnits(--unit._value, unit._unit); } //Prefix
NumberWithUnits operator--(NumberWithUnits &unit, int) { return NumberWithUnits(unit._value++, unit._unit); } //Postfix
NumberWithUnits operator*(const NumberWithUnits &unit, double num) { return NumberWithUnits(num * unit._value, unit._unit); }
NumberWithUnits operator*(double num, const NumberWithUnits &unit) { return NumberWithUnits(num * unit._value, unit._unit); }
ostream &operator<<(const ostream &os, const NumberWithUnits &unit) { return cout << unit._value << "[" << unit._unit << "]" << endl; }
istream &operator>>(std::istream &in, NumberWithUnits &unit)
{
string s;
in >> unit._value >> s >> unit._unit;
return in;
}
}
19 changes: 11 additions & 8 deletions NumberWithUnits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@ namespace ariel{
}
NumberWithUnits(){}
~NumberWithUnits(){}
static void read_units(const ifstream& file);
friend int compare(const NumberWithUnits& n1, const NumberWithUnits& n2);
double convert(const string &from, const string &to, double fromVal);
double convertHelp(const string &from, const string &to, double fromVal);
static void read_units(ifstream& file);
friend NumberWithUnits operator+(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend NumberWithUnits operator+=(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend NumberWithUnits operator+(const NumberWithUnits& unit);
friend NumberWithUnits operator+=(NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend NumberWithUnits operator+(const NumberWithUnits& unit, double num);
friend NumberWithUnits operator-(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend NumberWithUnits operator-=(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend NumberWithUnits operator-=(NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend NumberWithUnits operator-(const NumberWithUnits& unit);
friend bool operator>(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend bool operator>=(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend bool operator<(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend bool operator<=(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend bool operator==(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend bool operator!=(const NumberWithUnits& unit1, const NumberWithUnits& unit2);
friend NumberWithUnits operator++(const NumberWithUnits& unit); //Prefix
friend NumberWithUnits operator++(const NumberWithUnits& unit, int); //Postfix
friend NumberWithUnits operator--(const NumberWithUnits& unit); //Prefix
friend NumberWithUnits operator--(const NumberWithUnits& unit, int); //Postfix
friend NumberWithUnits operator++( NumberWithUnits& unit); //Prefix
friend NumberWithUnits operator++( NumberWithUnits& unit, int); //Postfix
friend NumberWithUnits operator--( NumberWithUnits& unit); //Prefix
friend NumberWithUnits operator--( NumberWithUnits& unit, int); //Postfix
friend NumberWithUnits operator*(const NumberWithUnits& unit, double num);
friend NumberWithUnits operator*(double num, const NumberWithUnits& unit);
friend ostream& operator<<(const ostream& os, const NumberWithUnits& unit);
Expand Down
23 changes: 12 additions & 11 deletions Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ static vector<string> units = {"m"};
ifstream units_file{"units.txt"};

TEST_CASE("Multi operators"){
NumberWithUnits::read_units(units_file);
NumberWithUnits a{1, "m"};
NumberWithUnits b{3, "m"};
NumberWithUnits c{300, "cm"};
Expand Down Expand Up @@ -42,17 +43,18 @@ TEST_CASE("All bool operators"){
CHECK(d>=c);
CHECK(c>=e);
CHECK(e>=c);
CHECK(a<d);
CHECK(c<d);
NumberWithUnits f{1, "kg"};
NumberWithUnits g{1000, "g"};
CHECK_FALSE(b!=a);
CHECK_FALSE(f!=g);
CHECK(g==f);
CHECK(g<=f);
NumberWithUnits h{0.0001, "ton"};
NumberWithUnits h{0.001, "ton"};
CHECK(f==h);
CHECK(f==g);
NumberWithUnits i{0.0002, "ton"};
NumberWithUnits i{0.002, "ton"};
CHECK(f!=i);
g++;
CHECK(f!=g);
}

Expand All @@ -67,13 +69,13 @@ TEST_CASE("+, -, +="){
CHECK((b+=a)==c);
CHECK((a+=c)==d);
CHECK_FALSE((b+=a)==d);
NumberWithUnits e{0.5, "km"};
NumberWithUnits e{1, "km"};
NumberWithUnits a2{2, "km"};
NumberWithUnits b2{1000, "m"};
CHECK((a2-b2)==e);
NumberWithUnits f{500, "m"};
CHECK((b2-a2)==e);
CHECK((b2-a2)==f);
CHECK((b2-a2)==-e);
CHECK((b2-a2)==-(f*2));
}

TEST_CASE("Postfix and Prefix operators (++/--)"){
Expand All @@ -82,10 +84,9 @@ TEST_CASE("Postfix and Prefix operators (++/--)"){
NumberWithUnits c{180, "min"};
CHECK(++a==b);
CHECK(++a==c);
CHECK(a++==a);
NumberWithUnits d{3, "hour"};
CHECK_FALSE(a++==a);
NumberWithUnits d{4, "hour"};
CHECK(a==d);
CHECK(--a==++b);
CHECK(a--==b);
}

}
3 changes: 3 additions & 0 deletions number-with-units-a.rar:Zone.Identifier
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
HostUrl=about:internet

0 comments on commit cf4ead6

Please sign in to comment.