forked from matanb1238/NumberWithUnits-A
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97aa06c
commit 26eae8b
Showing
3 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
#include <sstream> | ||
#include <stdexcept> | ||
#include "NumberWithUnits.hpp" | ||
// #define BOOL true | ||
|
||
// using namespace std; | ||
|
||
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <sstream> | ||
using namespace std; | ||
|
||
namespace ariel{ | ||
class NumberWithUnits{ | ||
private: | ||
double _value; | ||
string _unit; | ||
public: | ||
const bool BOOL = true; | ||
NumberWithUnits(double value, string unit){ | ||
_value=value; | ||
_unit=unit; | ||
} | ||
NumberWithUnits(){} | ||
~NumberWithUnits(){} | ||
static void read_units(const 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-(const NumberWithUnits& unit1, const NumberWithUnits& unit2); | ||
friend NumberWithUnits operator-=(const 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*(const NumberWithUnits& unit, double num); | ||
friend NumberWithUnits operator*(double num, const NumberWithUnits& unit); | ||
friend ostream& operator<<(const ostream& os, const NumberWithUnits& unit); | ||
friend istream& operator>>(istream& in, NumberWithUnits& unit); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "NumberWithUnits.hpp" | ||
#include "doctest.h" | ||
#include <iostream> | ||
#include <string> | ||
#include <fstream> | ||
#include <sstream> | ||
#include <ctime> | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
using namespace ariel; | ||
static vector<string> units = {"m"}; | ||
ifstream units_file{"units.txt"}; | ||
|
||
TEST_CASE("Multi operators"){ | ||
NumberWithUnits a{1, "m"}; | ||
NumberWithUnits b{3, "m"}; | ||
NumberWithUnits c{300, "cm"}; | ||
double num = 3; | ||
CHECK(a*num==b); | ||
CHECK(a*num==c); | ||
CHECK(num*a==b); | ||
CHECK(num*a==c); | ||
double number = 2; | ||
CHECK_FALSE(a*number==b); | ||
CHECK_FALSE(a*number==c); | ||
CHECK(number*a!=b); | ||
CHECK(number*a!=c); | ||
} | ||
|
||
TEST_CASE("All bool operators"){ | ||
NumberWithUnits a{110, "cm"}; | ||
NumberWithUnits b{1, "m"}; | ||
CHECK(a>b); | ||
CHECK(a>=b); | ||
CHECK(b<=a); | ||
CHECK(b<a); | ||
NumberWithUnits c{1, "hour"}; | ||
NumberWithUnits d{3601, "sec"}; | ||
NumberWithUnits e{60, "min"}; | ||
CHECK(d>c); | ||
CHECK(d>=c); | ||
CHECK(c>=e); | ||
CHECK(e>=c); | ||
CHECK(a<d); | ||
NumberWithUnits f{1, "kg"}; | ||
NumberWithUnits g{1000, "g"}; | ||
CHECK_FALSE(b!=a); | ||
CHECK(g==f); | ||
CHECK(g<=f); | ||
NumberWithUnits h{0.0001, "ton"}; | ||
CHECK(f==h); | ||
CHECK(f==g); | ||
NumberWithUnits i{0.0002, "ton"}; | ||
CHECK(f!=i); | ||
CHECK(f!=g); | ||
} | ||
|
||
TEST_CASE("+, -, +="){ | ||
NumberWithUnits::read_units(units_file); | ||
NumberWithUnits a{2, "km"}; | ||
NumberWithUnits b{1000, "m"}; | ||
NumberWithUnits c{3000, "m"}; | ||
CHECK((b+a)==c); | ||
NumberWithUnits d{5, "km"}; | ||
CHECK((a+b)==c); | ||
CHECK((b+=a)==c); | ||
CHECK((a+=c)==d); | ||
CHECK_FALSE((b+=a)==d); | ||
NumberWithUnits e{0.5, "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); | ||
} | ||
|
||
TEST_CASE("Postfix and Prefix operators (++/--)"){ | ||
NumberWithUnits a{1, "hour"}; | ||
NumberWithUnits b{2, "hour"}; | ||
NumberWithUnits c{180, "min"}; | ||
CHECK(++a==b); | ||
CHECK(++a==c); | ||
CHECK(a++==a); | ||
NumberWithUnits d{3, "hour"}; | ||
CHECK(a==d); | ||
CHECK(--a==++b); | ||
CHECK(a--==b); | ||
} | ||
|