Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create xman375_TestCollatz.c++ #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions xman375_TestCollatz.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// --------------------------------
// projects/collatz/TestCollatz.c++
// Copyright (C) 2013
// Glenn P. Downing
// --------------------------------

/*
To test the program:
% ls -al /usr/include/gtest/
...
gtest.h
...

% locate libgtest.a
/usr/lib/libgtest.a

% locate libpthread.a
/usr/lib/x86_64-linux-gnu/libpthread.a
/usr/lib32/libpthread.a

% locate libgtest_main.a
/usr/lib/libgtest_main.a

% g++ -pedantic -std=c++0x -Wall Collatz.c++ TestCollatz.c++ -o TestCollatz -lgtest -lpthread -lgtest_main

% valgrind TestCollatz > TestCollatz.out
*/

// --------
// includes
// --------

#include <iostream> // cout, endl
#include <sstream> // istringtstream, ostringstream
#include <string> // ==

#include "gtest/gtest.h"

#include "Collatz.h"

// -----------
// TestCollatz
// -----------

// ----
// read
// ----

TEST(Collatz, read) {
std::istringstream r("1 10\n");
int i;
int j;
const bool b = collatz_read(r, i, j);
ASSERT_TRUE(b == true);
ASSERT_TRUE(i == 1);
ASSERT_TRUE(j == 10);}

// -----
// cycle
// -----

TEST(Collatz, cycle_1)
{
const int v = collatz_cycle(15);
ASSERT_TRUE(v==18);
}

TEST(Collatz, cycle_2)
{
const int v = collatz_cycle(15);
ASSERT_TRUE(v==18);
}

TEST(Collatz, cycle_3)
{
const int v = collatz_cycle(318974);
ASSERT_TRUE(v==1);
}

// -------
// c_cycle
// -------

TEST(Collatz, c_cycle_1) {
int a[]={113, 51, 144};
const int v=collatz_c_cycle(4000, a, 2000);
ASSERT_TRUE(v==114);}

TEST(Collatz, c_cycle_2) {
int a[]={88, 87};
const int v=collatz_c_cycle(11, a, 10);
ASSERT_TRUE(v==87);}

TEST(Collatz, c_cycle_3) {
int a[]={113, 152, 139, 152, 258};
const int v=collatz_c_cycle(500008, a, 500007);
ASSERT_TRUE(v==152);}


// ----
// eval
// ----

TEST(Collatz, eval_1) {
const int v = collatz_eval(1, 10);
ASSERT_TRUE(v == 20);}

TEST(Collatz, eval_2) {
const int v = collatz_eval(100, 200);
ASSERT_TRUE(v == 125);}

TEST(Collatz, eval_3) {
const int v = collatz_eval(201, 210);
ASSERT_TRUE(v == 89);}

TEST(Collatz, eval_4) {
const int v = collatz_eval(900, 1000);
ASSERT_TRUE(v == 174);}

// -----
// print
// -----

TEST(Collatz, print) {
std::ostringstream w;
collatz_print(w, 1, 10, 20);
ASSERT_TRUE(w.str() == "1 10 20\n");}

// -----
// solve
// -----

TEST(Collatz, solve) {
std::istringstream r("1 10\n100 200\n201 210\n900 1000\n");
std::ostringstream w;
collatz_solve(r, w);
ASSERT_TRUE(w.str() == "1 10 20\n100 200 125\n201 210 89\n900 1000 174\n");}