From d2043dc715dac53f48379bf21b82529a95cc8378 Mon Sep 17 00:00:00 2001 From: Justin Lopez <116849803+justinlopez1@users.noreply.github.com> Date: Sun, 2 Jun 2024 16:23:10 -0400 Subject: [PATCH] refractor science module (#628) --- CMakeLists.txt | 1 + include/faker-cxx/Science.h | 12 +- src/modules/science/Science.cpp | 20 ++- src/modules/science/ScienceData.cpp | 170 ++++++++++++++++++++ src/modules/science/ScienceData.h | 15 ++ src/modules/science/data/ChemicalElements.h | 40 ----- src/modules/science/data/Units.h | 36 ----- tests/modules/science/ScienceTest.cpp | 6 +- 8 files changed, 204 insertions(+), 96 deletions(-) create mode 100644 src/modules/science/ScienceData.cpp create mode 100644 src/modules/science/ScienceData.h delete mode 100644 src/modules/science/data/ChemicalElements.h delete mode 100644 src/modules/science/data/Units.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f4380900d..28307d1e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/faker-cxx/Science.h b/include/faker-cxx/Science.h index f7dd6c9fb..9507839d3 100644 --- a/include/faker-cxx/Science.h +++ b/include/faker-cxx/Science.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace faker { @@ -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; }; @@ -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; }; /** diff --git a/src/modules/science/Science.cpp b/src/modules/science/Science.cpp index 72d539691..894f9d9d4 100644 --- a/src/modules/science/Science.cpp +++ b/src/modules/science/Science.cpp @@ -1,21 +1,19 @@ #include "faker-cxx/Science.h" -#include - -#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(chemicalElements); + return Helper::arrayElement(chemicalElements); } Science::Unit Science::unit() { - std::vector units = distanceUnits; + std::vector 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()); @@ -26,27 +24,27 @@ Science::Unit Science::unit() Science::Unit Science::distanceUnit() { - return Helper::arrayElement(distanceUnits); + return Helper::arrayElement(distanceUnits); } Science::Unit Science::timeUnit() { - return Helper::arrayElement(timeUnits); + return Helper::arrayElement(timeUnits); } Science::Unit Science::massUnit() { - return Helper::arrayElement(massUnits); + return Helper::arrayElement(massUnits); } Science::Unit Science::tempUnit() { - return Helper::arrayElement(temperatureUnits); + return Helper::arrayElement(temperatureUnits); } Science::Unit Science::currentUnit() { - return Helper::arrayElement(currentUnits); + return Helper::arrayElement(currentUnits); } } diff --git a/src/modules/science/ScienceData.cpp b/src/modules/science/ScienceData.cpp new file mode 100644 index 000000000..9761bd979 --- /dev/null +++ b/src/modules/science/ScienceData.cpp @@ -0,0 +1,170 @@ +#include "ScienceData.h" + +namespace faker { + +const std::array 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 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 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 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 currentUnits = {{ + {"Ampere", "A", "Electric Current"}, + {"Milliampere", "mA", "Electric Current"}, + {"Microampere", "μA", "Electric Current"}, +}}; + +const std::array temperatureUnits = {{ + {"Celcius", "°C", "Temperature"}, + {"Fahrenheit", "°F", "Temperature"}, + {"Kelvin", "K", "Temperature"}, +}}; + +} diff --git a/src/modules/science/ScienceData.h b/src/modules/science/ScienceData.h new file mode 100644 index 000000000..bba03df89 --- /dev/null +++ b/src/modules/science/ScienceData.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "faker-cxx/Science.h" + +namespace faker +{ +extern const std::array chemicalElements; +extern const std::array distanceUnits; +extern const std::array massUnits; +extern const std::array timeUnits; +extern const std::array currentUnits; +extern const std::array temperatureUnits; +} diff --git a/src/modules/science/data/ChemicalElements.h b/src/modules/science/data/ChemicalElements.h deleted file mode 100644 index 4edf1dfdb..000000000 --- a/src/modules/science/data/ChemicalElements.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include - -#include "faker-cxx/Science.h" - -namespace faker -{ -const std::vector 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}}; -} diff --git a/src/modules/science/data/Units.h b/src/modules/science/data/Units.h deleted file mode 100644 index b716e2fdf..000000000 --- a/src/modules/science/data/Units.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include - -#include "faker-cxx/Science.h" - -namespace faker -{ -const std::vector 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::vector 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::vector timeUnits = { - {"Second", "s", "Time"}, {"Minute", "min", "Time"}, {"Hour", "hr", "Time"}, {"Day", "d", "Time"}, - {"Week", "wk", "Time"}, {"Month", "mo", "Time"}, {"Year", "yr", "Time"}, -}; - -const std::vector currentUnits = { - {"Ampere", "A", "Electric Current"}, - {"Milliampere", "mA", "Electric Current"}, - {"Microampere", "μA", "Electric Current"}, -}; - -const std::vector temperatureUnits = { - {"Celcius", "°C", "Temperature"}, - {"Fahrenheit", "°F", "Temperature"}, - {"Kelvin", "K", "Temperature"}, -}; -} diff --git a/tests/modules/science/ScienceTest.cpp b/tests/modules/science/ScienceTest.cpp index 58cc723d1..e8e23c4f2 100644 --- a/tests/modules/science/ScienceTest.cpp +++ b/tests/modules/science/ScienceTest.cpp @@ -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; @@ -38,7 +37,8 @@ TEST_F(ScienceTest, shouldGenerateChemElement) TEST_F(ScienceTest, shouldGenerateAnyUnit) { const auto generatedAnyUnit = Science::unit(); - std::vector units = distanceUnits; + std::vector 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());