Skip to content

Commit

Permalink
Copied Bootcamp Structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Maftab27 committed Feb 16, 2024
1 parent 3efe7c2 commit 3000706
Show file tree
Hide file tree
Showing 29 changed files with 641 additions and 1 deletion.
41 changes: 41 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: OS Starter Project

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally
# well on Windows or Mac. You can convert this to a matrix build if you need
# cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install GNU pth (userland threads example)
run: sudo apt install libpth-dev

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
#run: ctest -C ${{env.BUILD_TYPE}}
run: for test_bin in bin/*tests; do "./$test_bin"; done
77 changes: 77 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
build/

# Backups
*~
*.orig
*.swp

# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

# Targets to ignore
bin/
lib/

# IntelliJ/CLion

.idea/
*-debug/
10 changes: 10 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: |
sudo apt update -q && sudo apt upgrade -y -q
sudo apt install cmake -y
cmake -DCMAKE_BUILD_TYPE=Debug -S . -B build
cmake --build build
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.5)

project(OperatingSystemsStarter)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(CMAKE_CXX_STANDARD 17)

include(FetchContent)

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.14.0
)

# Windows workaround (don't ask)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(googletest)
include_directories(${GTEST_INCLUDE_DIRS})

add_subdirectory(hello)
add_subdirectory(hello-lib)
add_subdirectory(lwlog-lib)
add_subdirectory(lwlog-demo)
add_subdirectory(gtest-demo)

# You can use add_subdirectory() to add your own work.
# Please don't remove the existing subdirectories.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
[![Open in Codespaces](https://classroom.github.com/assets/launch-codespace-7f7980b617ed060a017424585567c406b6ee15c891e84e1186181d67ecf80aa0.svg)](https://classroom.github.com/open-in-codespaces?assignment_repo_id=13588010)
38 changes: 38 additions & 0 deletions gtest-demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
square_root_app
square_root_app.c
square_root.c)

add_executable(
countbits_app
countbits_app.c
countbits.c)

add_executable(
square_root_tests
square_root_tests.cc
square_root.c)

add_executable(
countbits_tests
countbits_tests.cc
countbits.c)

target_link_libraries(square_root_app
-lpthread
-lm)

target_link_libraries(square_root_tests
gtest_main
-lpthread
-lm)

target_link_libraries(countbits_tests
gtest_main
-lpthread
-lm)
include(GoogleTest)
19 changes: 19 additions & 0 deletions gtest-demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Run Google Tests in Docker
# Any executable ending with _tests is automatically found (using find) and run
#

FROM coenvl/googletest

ENV BUILD /sandbox/build

WORKDIR /sandbox

ADD . /sandbox

RUN rm -f ${BUILD}/CMakeCache.txt && \
mkdir -p ${BUILD} && \
cd ${BUILD} && \
cmake .. && \
make && \
find ${BUILD} -name '*_tests' -exec {} \;
20 changes: 20 additions & 0 deletions gtest-demo/countbits.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* countbits(n) tells how many bits are set in the (unsigned) integer n.
*/


#include <stdio.h>
#include <stdlib.h>

#include "countbits.h"

int count_bits(unsigned long n)
{
int number_set=0;
unsigned long number = n;
while (number > 0) {
number_set += (number & 0x1);
number >>= 1;
}
return number_set;
}
5 changes: 5 additions & 0 deletions gtest-demo/countbits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifndef _COUNTBITS_H_

extern int count_bits(unsigned long n);

#endif
16 changes: 16 additions & 0 deletions gtest-demo/countbits.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// square_root.hh
// This header ensures C linkage is enforced when running C++ unit tessts on C code.

#include <math.h>

#ifdef __cplusplus

extern "C" {
#include "countbits.h"
}

#else

#include "countbits.h"

#endif
16 changes: 16 additions & 0 deletions gtest-demo/countbits_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* countbits(n) tells how many bits are set in the (unsigned) integer n.
*/


#include <stdio.h>
#include <stdlib.h>

#include "countbits.h"

int main(int argc, char *argv[]) {
for (int i=1; i < argc; i++) {
unsigned long value = atol(argv[i]);
printf("%ld has %d bits set\n", value, count_bits(value));
}
}
34 changes: 34 additions & 0 deletions gtest-demo/countbits_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* test cases */

#include <gtest/gtest.h>

#include "countbits.hh"

TEST(SquareRootTest, Simple) {

ASSERT_EQ(4, count_bits(0xF));
ASSERT_EQ(8, count_bits(0xFF));
ASSERT_EQ(32, count_bits(0xFFFFFFFF));
}

TEST(SquareRootTest, Complex) {
unsigned long values[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
int bits[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };

int num_values = sizeof(values) / sizeof(unsigned long);
for (int i=0; i < num_values; i++) {
ASSERT_EQ(bits[i], count_bits(values[i]));
}

// use individual nibbles (hex digits) to generate a 32-bit value and see if bits add up
unsigned long value = values[0] + (values[3] << 8) + (values[6] << 16) + (values[9] << 24);
unsigned long value_bits = bits[0] + bits[3] + bits[6] + bits[9];
ASSERT_EQ(value_bits, count_bits(value));


}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
14 changes: 14 additions & 0 deletions gtest-demo/square_root.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* module to be tested */

#include <math.h>

#include "square_root.h"

double square_root(const double a) {
double b = sqrt(a);
if(b != b) { // nan check
return -1.0;
} else {
return sqrt(a);
}
}
5 changes: 5 additions & 0 deletions gtest-demo/square_root.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// square_root.cc

#include <math.h>

extern double square_root(const double a);
15 changes: 15 additions & 0 deletions gtest-demo/square_root.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// square_root.hh
// This header ensures C linkage is enforced when running C++ unit tessts on C code.

#include <math.h>

#ifdef __cplusplus
extern "C" {
#include "square_root.h"
}

#else

#include "square_root.h"

#endif
9 changes: 9 additions & 0 deletions gtest-demo/square_root_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* standalone app not requiring tests to run */

#include <stdio.h>

#include "square_root.h"

int main(int argc, char* argv[]) {
printf("sqrt(2) = %.5f\n", square_root(2.0));
}
Loading

0 comments on commit 3000706

Please sign in to comment.