Skip to content

Commit

Permalink
refractor science module (cieslarmichal#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlopez1 authored and 00thirdeye00 committed Jun 20, 2024
1 parent 3f0c444 commit d2043dc
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 96 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(FAKER_SOURCES
src/modules/phone/Phone.cpp
src/modules/phone/PhoneData.cpp
src/modules/science/Science.cpp
src/modules/science/ScienceData.cpp
src/modules/sport/Sport.cpp
src/modules/sport/SportData.cpp
src/modules/string/String.cpp
Expand Down
12 changes: 6 additions & 6 deletions include/faker-cxx/Science.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <string_view>

namespace faker
{
Expand All @@ -9,8 +9,8 @@ class Science
public:
struct ChemicalElement
{
std::string name;
std::string symbol;
std::string_view name;
std::string_view symbol;
int atomicNumber;
};

Expand All @@ -28,9 +28,9 @@ class Science

struct Unit
{
std::string name;
std::string symbol;
std::string usedToMeasure;
std::string_view name;
std::string_view symbol;
std::string_view usedToMeasure;
};

/**
Expand Down
20 changes: 9 additions & 11 deletions src/modules/science/Science.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#include "faker-cxx/Science.h"

#include <vector>

#include "data/ChemicalElements.h"
#include "data/Units.h"
#include "ScienceData.h"
#include "faker-cxx/Helper.h"

namespace faker
{
Science::ChemicalElement Science::chemicalElement()
{
return Helper::arrayElement<Science::ChemicalElement>(chemicalElements);
return Helper::arrayElement(chemicalElements);
}

Science::Unit Science::unit()
{
std::vector<faker::Science::Unit> units = distanceUnits;
std::vector<faker::Science::Unit> units;
units.insert(units.end(), distanceUnits.begin(), distanceUnits.end());
units.insert(units.end(), massUnits.begin(), massUnits.end());
units.insert(units.end(), timeUnits.begin(), timeUnits.end());
units.insert(units.end(), currentUnits.begin(), currentUnits.end());
Expand All @@ -26,27 +24,27 @@ Science::Unit Science::unit()

Science::Unit Science::distanceUnit()
{
return Helper::arrayElement<Science::Unit>(distanceUnits);
return Helper::arrayElement(distanceUnits);
}

Science::Unit Science::timeUnit()
{
return Helper::arrayElement<Science::Unit>(timeUnits);
return Helper::arrayElement(timeUnits);
}

Science::Unit Science::massUnit()
{
return Helper::arrayElement<Science::Unit>(massUnits);
return Helper::arrayElement(massUnits);
}

Science::Unit Science::tempUnit()
{
return Helper::arrayElement<Science::Unit>(temperatureUnits);
return Helper::arrayElement(temperatureUnits);
}

Science::Unit Science::currentUnit()
{
return Helper::arrayElement<Science::Unit>(currentUnits);
return Helper::arrayElement(currentUnits);
}

}
170 changes: 170 additions & 0 deletions src/modules/science/ScienceData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#include "ScienceData.h"

namespace faker {

const std::array<Science::ChemicalElement, 118> chemicalElements = {{
{"Hydrogen", "H", 1},
{"Helium", "He", 2},
{"Lithium", "Li", 3},
{"Beryllium", "Be", 4},
{"Boron", "B", 5},
{"Carbon", "C", 6},
{"Nitrogen", "N", 7},
{"Oxygen", "O", 8},
{"Fluorine", "F", 9},
{"Neon", "Ne", 10},
{"Sodium", "Na", 11},
{"Magnesium", "Mg", 12},
{"Aluminum", "Al", 13},
{"Silicon", "Si", 14},
{"Phosphorus", "P", 15},
{"Sulfur", "S", 16},
{"Chlorine", "Cl", 17},
{"Argon", "Ar", 18},
{"Potassium", "K", 19},
{"Calcium", "Ca", 20},
{"Scandium", "Sc", 21},
{"Titanium", "Ti", 22},
{"Vanadium", "V", 23},
{"Chromium", "Cr", 24},
{"Manganese", "Mn", 25},
{"Iron", "Fe", 26},
{"Cobalt", "Co", 27},
{"Nickel", "Ni", 28},
{"Copper", "Cu", 29},
{"Zinc", "Zn", 30},
{"Gallium", "Ga", 31},
{"Germanium", "Ge", 32},
{"Arsenic", "As", 33},
{"Selenium", "Se", 34},
{"Bromine", "Br", 35},
{"Krypton", "Kr", 36},
{"Rubidium", "Rb", 37},
{"Strontium", "Sr", 38},
{"Yttrium", "Y", 39},
{"Zirconium", "Zr", 40},
{"Niobium", "Nb", 41},
{"Molybdenum", "Mo", 42},
{"Technetium", "Tc", 43},
{"Ruthenium", "Ru", 44},
{"Rhodium", "Rh", 45},
{"Palladium", "Pd", 46},
{"Silver", "Ag", 47},
{"Cadmium", "Cd", 48},
{"Indium", "In", 49},
{"Tin", "Sn", 50},
{"Antimony", "Sb", 51},
{"Tellurium", "Te", 52},
{"Iodine", "I", 53},
{"Xenon", "Xe", 54},
{"Cesium", "Cs", 55},
{"Barium", "Ba", 56},
{"Lanthanum", "La", 57},
{"Cerium", "Ce", 58},
{"Praseodymium", "Pr", 59},
{"Neodymium", "Nd", 60},
{"Promethium", "Pm", 61},
{"Samarium", "Sm", 62},
{"Europium", "Eu", 63},
{"Gadolinium", "Gd", 64},
{"Terbium", "Tb", 65},
{"Dysprosium", "Dy", 66},
{"Holmium", "Ho", 67},
{"Erbium", "Er", 68},
{"Thulium", "Tm", 69},
{"Ytterbium", "Yb", 70},
{"Lutetium", "Lu", 71},
{"Hafnium", "Hf", 72},
{"Tantalum", "Ta", 73},
{"Tungsten", "W", 74},
{"Rhenium", "Re", 75},
{"Osmium", "Os", 76},
{"Iridium", "Ir", 77},
{"Platinum", "Pt", 78},
{"Gold", "Au", 79},
{"Mercury", "Hg", 80},
{"Thallium", "Tl", 81},
{"Lead", "Pb", 82},
{"Bismuth", "Bi", 83},
{"Polonium", "Po", 84},
{"Astatine", "At", 85},
{"Radon", "Rn", 86},
{"Francium", "Fr", 87},
{"Radium", "Ra", 88},
{"Actinium", "Ac", 89},
{"Thorium", "Th", 90},
{"Protactinium", "Pa", 91},
{"Uranium", "U", 92},
{"Neptunium", "Np", 93},
{"Plutonium", "Pu", 94},
{"Americium", "Am", 95},
{"Curium", "Cm", 96},
{"Berkelium", "Bk", 97},
{"Californium", "Cf", 98},
{"Einsteinium", "Es", 99},
{"Fermium", "Fm", 100},
{"Mendelevium", "Md", 101},
{"Nobelium", "No", 102},
{"Lawrencium", "Lr", 103},
{"Rutherfordium", "Rf", 104},
{"Dubnium", "Db", 105},
{"Seaborgium", "Sg", 106},
{"Bohrium", "Bh", 107},
{"Hassium", "Hs", 108},
{"Meitnerium", "Mt", 109},
{"Darmstadtium", "Ds", 110},
{"Roentgenium", "Rg", 111},
{"Copernicium", "Cn", 112},
{"Nihonium", "Nh", 113},
{"Flerovium", "Fl", 114},
{"Moscovium", "Mc", 115},
{"Livermorium", "Lv", 116},
{"Tennessine", "Ts", 117},
{"Oganesson", "Og", 118}
}};

const std::array<Science::Unit, 8> distanceUnits = {{
{"Millimeter", "mm", "Length"},
{"Centimeter", "cm", "Length"},
{"Meter", "m", "Length"},
{"Kilometer", "km", "Length"},
{"Inch", "in", "Length"},
{"Foot", "ft", "Length"},
{"Yard", "yd", "Length"},
{"Mile", "mi", "Length"},
}};

const std::array<Science::Unit, 8> massUnits = {{
{"Gram", "g", "Mass"},
{"Kilogram", "kg", "Mass"},
{"Milligram", "mg", "Mass"},
{"Microgram", "μg", "Mass"},
{"Metric ton", "t", "Mass"},
{"Ounce", "oz", "Mass"},
{"Pound", "lb", "Mass"},
{"Slug", "sl", "Mass"},
}};

const std::array<Science::Unit, 7> timeUnits = {{
{"Second", "s", "Time"},
{"Minute", "min", "Time"},
{"Hour", "hr", "Time"},
{"Day", "d", "Time"},
{"Week", "wk", "Time"},
{"Month", "mo", "Time"},
{"Year", "yr", "Time"},
}};

const std::array<Science::Unit, 3> currentUnits = {{
{"Ampere", "A", "Electric Current"},
{"Milliampere", "mA", "Electric Current"},
{"Microampere", "μA", "Electric Current"},
}};

const std::array<Science::Unit, 3> temperatureUnits = {{
{"Celcius", "°C", "Temperature"},
{"Fahrenheit", "°F", "Temperature"},
{"Kelvin", "K", "Temperature"},
}};

}
15 changes: 15 additions & 0 deletions src/modules/science/ScienceData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <array>

#include "faker-cxx/Science.h"

namespace faker
{
extern const std::array<Science::ChemicalElement, 118> chemicalElements;
extern const std::array<Science::Unit, 8> distanceUnits;
extern const std::array<Science::Unit, 8> massUnits;
extern const std::array<Science::Unit, 7> timeUnits;
extern const std::array<Science::Unit, 3> currentUnits;
extern const std::array<Science::Unit, 3> temperatureUnits;
}
40 changes: 0 additions & 40 deletions src/modules/science/data/ChemicalElements.h

This file was deleted.

36 changes: 0 additions & 36 deletions src/modules/science/data/Units.h

This file was deleted.

6 changes: 3 additions & 3 deletions tests/modules/science/ScienceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

#include "gtest/gtest.h"

#include "science/data/ChemicalElements.h"
#include "science/data/Units.h"
#include "science/ScienceData.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -38,7 +37,8 @@ TEST_F(ScienceTest, shouldGenerateChemElement)
TEST_F(ScienceTest, shouldGenerateAnyUnit)
{
const auto generatedAnyUnit = Science::unit();
std::vector<faker::Science::Unit> units = distanceUnits;
std::vector<faker::Science::Unit> units;
units.insert(units.end(), distanceUnits.begin(), distanceUnits.end());
units.insert(units.end(), massUnits.begin(), massUnits.end());
units.insert(units.end(), timeUnits.begin(), timeUnits.end());
units.insert(units.end(), currentUnits.begin(), currentUnits.end());
Expand Down

0 comments on commit d2043dc

Please sign in to comment.