Skip to content

Commit

Permalink
test: Added more tests for normal distribution functions (#885)
Browse files Browse the repository at this point in the history
* Added a feature to allow the generation of a random number on a normal distribution

Added two functions to number.h, which are overloads of each other. One will generate a number on a normal distribution given a mean and standard deviation, the other does the same but limits the output to a certain minimum and maximum value

Added the relevant unit tests to number_test.cpp

* Update number.h

* added requested changes

* Expanded on binary string generation functionality

-Added functionality to generate a binary string which has a decimal equivalent within a specified range

-Removed use of unisgned integers, as they can lead to unintended overflows

* Update string.cpp

* Added more tests for normal distribution

* asd

* wordVector function added
  • Loading branch information
pshukla441 authored Sep 6, 2024
1 parent c3c7819 commit 766ae4a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/faker-cxx/lorem.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
#pragma once

#include <string>
#include <vector>

#include "faker-cxx/export.h"

namespace faker::lorem
{
/**
* @brief Generates and returns a dynamic array consisting of random lorem words.
*
* @returns A std::vector consisting of random lorem words
*
* @param numberOfWords The number of elements in the vector which is returned
*
* @throws std::invalid_argument if the numberOfWords is less than 0
*
* @code
* faker::lorem::wordVector(3) // "temporibus", "ipsum", "alias"
* @endcode
*/
FAKER_CXX_EXPORT std::vector<std::string_view> wordVector(int numberOfWords = 3);

/**
* @brief Returns a random lorem word.
*
Expand Down
16 changes: 16 additions & 0 deletions src/modules/lorem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@

namespace faker::lorem
{
std::vector<std::string_view> wordVector(int numberOfWords)
{
if(numberOfWords < 0)
{
throw std::invalid_argument("The number of words cannot be negative");
}

std::vector<std::string_view> output;
for(int i = 0; i < numberOfWords; i++)
{
output.push_back(helper::randomElement(loremWords));
}

return output;
}

std::string_view word()
{
return helper::randomElement(loremWords);
Expand Down
31 changes: 31 additions & 0 deletions tests/modules/lorem_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,37 @@ class LoremTest : public Test
public:
};

TEST_F(LoremTest, shouldGenerateWordVector)
{
const int numberOfWords = 100;
const auto generatedWords = wordVector(numberOfWords);

ASSERT_EQ(generatedWords.size(), numberOfWords);

ASSERT_TRUE
(
std::ranges::all_of(generatedWords,
[](const std::string_view& generatedWord)
{
return std::ranges::any_of (
loremWords, [generatedWord](const std::string_view& loremWord)
{
return loremWord == generatedWord;
});
}));
}

TEST_F(LoremTest, givenInvalidArguments_shouldThrowInvalidArgument)
{
ASSERT_THROW(wordVector(-1), std::invalid_argument);
}

TEST_F(LoremTest, givenLengthOf0_shouldReturnEmptyVector)
{
const auto generatedWords = wordVector(0);
ASSERT_TRUE(generatedWords.empty());
}

TEST_F(LoremTest, shouldGenerateWord)
{
const auto generatedWord = word();
Expand Down
14 changes: 14 additions & 0 deletions tests/modules/number_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,18 @@ TEST_F(NumberTest, givenRangeWithSameNumberSection_shouldGenerateTheExactNumber)
const std::floating_point auto normalDistributionNumber = normalDistribution<float>(10.f, 1000.f, 12.f, 12.f);

ASSERT_TRUE(normalDistributionNumber == 12.f);
}

TEST_F(NumberTest, givenHighRange_shouldGenerateMin)
{
const std::floating_point auto normalDistributionNumber = normalDistribution<float>(-100, .0001f, 10000.f, 10001.f);

ASSERT_TRUE(normalDistributionNumber == 10000.f);
}

TEST_F(NumberTest, givenHighRange_shouldGenerateMax)
{
const std::floating_point auto normalDistributionNum = normalDistribution<float>(10000, .0001f, -10001.f, -10000.f);

ASSERT_TRUE(normalDistributionNum == -10000.f);
}

0 comments on commit 766ae4a

Please sign in to comment.