Skip to content

Commit

Permalink
Add exercise 1-5
Browse files Browse the repository at this point in the history
  • Loading branch information
winswu committed Nov 10, 2024
1 parent d6472b3 commit c7f6aaf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
16 changes: 16 additions & 0 deletions chapter_1/exercise_1_5/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cc_library(
name = "lib",
srcs = ["lib.c"],
hdrs = ["lib.h"],
)

cc_test(
name = "test",
size = "small",
srcs = ["test/test.cc"],
deps = [
":lib",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)
12 changes: 12 additions & 0 deletions chapter_1/exercise_1_5/lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

/* print Fahrenheit-Celsius table */
int exercise_1_5()
{
int fahr;

for (fahr = 300; fahr >= 0; fahr = fahr - 20)
printf("%3d %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));

return 0;
}
1 change: 1 addition & 0 deletions chapter_1/exercise_1_5/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int exercise_1_5();
36 changes: 36 additions & 0 deletions chapter_1/exercise_1_5/test/test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <gtest/gtest.h>

#include <sstream>
#include <string>

extern "C" {
#include "../lib.h"
}

TEST(Exercise_1_5, ReverseOrder) {
std::string line;

testing::internal::CaptureStdout();

exercise_1_5();

std::string output = testing::internal::GetCapturedStdout();

EXPECT_EQ(output,
"300 148.9\n"
"280 137.8\n"
"260 126.7\n"
"240 115.6\n"
"220 104.4\n"
"200 93.3\n"
"180 82.2\n"
"160 71.1\n"
"140 60.0\n"
"120 48.9\n"
"100 37.8\n"
" 80 26.7\n"
" 60 15.6\n"
" 40 4.4\n"
" 20 -6.7\n"
" 0 -17.8\n");
}

0 comments on commit c7f6aaf

Please sign in to comment.