Skip to content

Commit e774577

Browse files
committed
add and tested naive polynomial evaluation
new file: ch02/src/polynomial.hpp new file: ch02/test/test_polynomial.cpp
1 parent df01464 commit e774577

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

ch02/src/polynomial.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
namespace clrs
4+
{
5+
namespace ch02
6+
{
7+
template<typename Sequence>
8+
typename Sequence::value_type naive_evaluate(Sequence const& seq, typename Sequence::value_type x)
9+
{
10+
using Value = typename Sequence::value_type;
11+
12+
auto y = Value(0);
13+
for (int i = 0; i != seq.size(); ++i)
14+
{
15+
auto x_part = Value(1);
16+
for (auto k = i; k != 0; --k)
17+
x_part *= x;
18+
y += x_part * seq[i];
19+
}
20+
21+
return y;
22+
}
23+
}
24+
}

ch02/src/src.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<ClInclude Include="add_binary_numbers.hpp" />
6868
<ClInclude Include="insertion_sort.hpp" />
6969
<ClInclude Include="merge_sort.hpp" />
70+
<ClInclude Include="polynomial.hpp" />
7071
<ClInclude Include="selection_sort.hpp" />
7172
</ItemGroup>
7273
<ItemGroup>

ch02/src/src.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<ClInclude Include="merge_sort.hpp">
2828
<Filter>Header Files</Filter>
2929
</ClInclude>
30+
<ClInclude Include="polynomial.hpp">
31+
<Filter>Header Files</Filter>
32+
</ClInclude>
3033
</ItemGroup>
3134
<ItemGroup>
3235
<ClCompile Include="main.cpp">

ch02/test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<ClCompile Include="test_add_binary_numbers.cpp" />
9494
<ClCompile Include="test_insertion_sort.cpp" />
9595
<ClCompile Include="test_merge_sort.cpp" />
96+
<ClCompile Include="test_polynomial.cpp" />
9697
<ClCompile Include="test_selection_sort.cpp" />
9798
</ItemGroup>
9899
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

ch02/test/test.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@
3838
<ClCompile Include="test_merge_sort.cpp">
3939
<Filter>Source Files</Filter>
4040
</ClCompile>
41+
<ClCompile Include="test_polynomial.cpp">
42+
<Filter>Source Files</Filter>
43+
</ClCompile>
4144
</ItemGroup>
4245
</Project>

ch02/test/test_polynomial.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "stdafx.h"
2+
#include "CppUnitTest.h"
3+
#include "../src/polynomial.hpp"
4+
#include <vector>
5+
6+
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
7+
8+
namespace test
9+
{
10+
TEST_CLASS(test_polynomial)
11+
{
12+
public:
13+
14+
TEST_METHOD(naive_evaluate_case1)
15+
{
16+
auto sample = std::vector < int > { 1 }; //y = 1
17+
auto x = 2;
18+
auto expect = 1;
19+
auto actual = clrs::ch02::naive_evaluate(sample, x);
20+
21+
Assert::AreEqual(actual, expect);
22+
}
23+
24+
TEST_METHOD(naive_evaluate_case2)
25+
{
26+
auto sample = std::vector < int > { 1, 2 }; // y = 1 + 2x
27+
auto x = 2;
28+
auto expect = 5;
29+
auto actual = clrs::ch02::naive_evaluate(sample, x);
30+
31+
Assert::AreEqual(actual, expect);
32+
}
33+
34+
TEST_METHOD(naive_evaluate_case3)
35+
{
36+
auto sample = std::vector < int > { 5, 2, 0, 7}; // y = 5 + 2x + 0x^2 + 7x^3
37+
auto x = 2; // = 5 + 4 + 0 + 56 = 65
38+
auto expect = 65;
39+
auto actual = clrs::ch02::naive_evaluate(sample, x);
40+
41+
Assert::AreEqual(expect, actual);
42+
}
43+
44+
};
45+
}

0 commit comments

Comments
 (0)