<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yanujz</title>
    <description>The latest articles on DEV Community by Yanujz (@yanujz).</description>
    <link>https://dev.to/yanujz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F411006%2F5797342e-6108-4a7f-973a-c90aac4a9609.jpeg</url>
      <title>DEV Community: Yanujz</title>
      <link>https://dev.to/yanujz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yanujz"/>
    <language>en</language>
    <item>
      <title>Getting started with GoogleTest and CMake</title>
      <dc:creator>Yanujz</dc:creator>
      <pubDate>Sun, 11 Aug 2024 20:03:30 +0000</pubDate>
      <link>https://dev.to/yanujz/getting-started-with-googletest-and-cmake-1kgg</link>
      <guid>https://dev.to/yanujz/getting-started-with-googletest-and-cmake-1kgg</guid>
      <description>&lt;p&gt;I wanted to get started with GoogleTest, and while searching for information, I came across this solution. I hope it can save you some time in your search.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A compatible operating system (e.g. Linux, macOS, Windows).&lt;/li&gt;
&lt;li&gt;A compatible C/C++ compiler&lt;/li&gt;
&lt;li&gt;CMake installed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Set up the project
&lt;/h2&gt;

&lt;p&gt;To get started with GoogleTest, follow these steps to set up your project structure. Below is the folder tree we'll use and a brief explanation of each component:&lt;/p&gt;

&lt;h3&gt;
  
  
  Folder tree
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;googletest-boilerplate              # Root directory of your project
├── CMakeLists.txt                  # Top-level CMake configuration file
├── src                             # Directory for source code
│   ├── CMakeLists.txt              # CMake configuration for source files
│   ├── example.c                   # C source file with implementation code
│   ├── example.h                   # Header file for example.c
│   └── main.c                      # Main source file
└── tests                           # Directory for test code
    ├── CMakeLists.txt              # CMake configuration for test files
    ├── test_example.cpp            # C++ file containing test cases
    └── test_main.cpp               # C++ file that sets up GoogleTest and runs tests

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s start with the top-level &lt;code&gt;CMakeLists.txt&lt;/code&gt; file. This file configures the overall project and includes the subdirectories for source and test files. Here’s what the content will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cmake_minimum_required(VERSION 3.14)  # Minimum CMake version required
project(googletest-boilerplate VERSION 0.1.0 LANGUAGES C CXX)  # Project name and version, specifying C and C++ languages

# Set the C and C++ standards
set(CMAKE_C_STANDARD 11)  # Set C standard to C11
set(CMAKE_C_STANDARD_REQUIRED True)  # Ensure the C standard is required

set(CMAKE_CXX_STANDARD 14)  # Set C++ standard to C++14
set(CMAKE_CXX_STANDARD_REQUIRED True)  # Ensure the C++ standard is required

# Add subdirectories for source and test files
add_subdirectory(src)  # Includes the src directory
add_subdirectory(tests)  # Includes the tests directory

include(CTest)  # Include CTest module for testing support
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Followed by &lt;code&gt;tests/CMakeLists.txt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;include(FetchContent)

# Fetch GoogleTest
FetchContent_Declare(
    googletest
    GIT_REPOSITORY https://github.com/google/googletest.git
    GIT_TAG        release-1.11.0
)
FetchContent_MakeAvailable(googletest)

# Collect  C++ source files recursively
file(GLOB_RECURSE CXX_FILES "${CMAKE_CURRENT_LIST_DIR}/*.cpp")
add_executable(unit_tests ${CXX_FILES})

# Link GoogleTest libraries
target_link_libraries(unit_tests
    PRIVATE
    gtest_main
    ${PROJECT_NAME}_lib  # Link to the main project library
)
# Include directories (including where GoogleTest is built)
target_include_directories(unit_tests PRIVATE ${gtest_SOURCE_DIR}/include)
# Add include directories for tests to find headers
target_include_directories(unit_tests PRIVATE ${PROJECT_SOURCE_DIR}/src)


# Enable testing and discover tests
# Discover and run tests
include(GoogleTest)
gtest_discover_tests(unit_tests)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Followed by &lt;code&gt;src/CMakeList.txt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Set the source directory to the current directory
set(SRC_DIR ${CMAKE_CURRENT_LIST_DIR})

# Recursively collect all .c and .h files in the source directory
file(GLOB_RECURSE SOURCES ${SRC_DIR}/*.c ${SRC_DIR}/*.h)

# Define the name of the main project library using the project name with a "_lib" suffix
set(MAIN_PROJECT_LIBNAME ${PROJECT_NAME}_lib)

# Create a static library from the collected source files
add_library(${MAIN_PROJECT_LIBNAME} STATIC
    ${SOURCES}
)

# Add the main executable, specifying 'main.c' as the source file
add_executable(${PROJECT_NAME} main.c)

# Link the main executable with the static library created earlier
target_link_libraries(${PROJECT_NAME} ${MAIN_PROJECT_LIBNAME})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;On the CMake side, we're done. Let's quickly take a look at the sources now.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tests/test_main.cpp&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;gtest/gtest.h&amp;gt;  // Include the GoogleTest framework header

// Dummy test
// Define a test case named 'ExampleTest' and a test named 'test'
TEST(ExampleTest, test)
{
    EXPECT_EQ(true, true);
}

int main(int argc, char **argv) {
    // Initialize the GoogleTest framework with command-line arguments
    ::testing::InitGoogleTest(&amp;amp;argc, argv);

    // Run all the tests that have been defined and return the result
    return RUN_ALL_TESTS();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: In the code below, we can access the functions from &lt;code&gt;example.h&lt;/code&gt; because we included the src directory in the include path by specifying the following line in &lt;code&gt;tests/CMakeLists.txt&lt;/code&gt; (line 24):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;target_include_directories(unit_tests PRIVATE ${PROJECT_SOURCE_DIR}/src)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your project has a more complex directory structure, make sure to include the necessary directories in the include path to ensure proper access to header files.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tests/test_example.cpp&lt;/code&gt;&lt;br&gt;
This will be the core test of our example. We'll call the functions inside the &lt;code&gt;src&lt;/code&gt; directory."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;gtest/gtest.h&amp;gt;
#include "example.h"


TEST(Example, sumTest)
{
    uint32_t a = 10;
    uint32_t b = 20;

    EXPECT_EQ(a + b, sum(a, b));
}

TEST(Example, squareTest)
{
    uint32_t a = 10;

    EXPECT_EQ(a * a, square(a));
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Source code files here below:&lt;br&gt;
&lt;code&gt;src/example.h&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#pragma once
#include &amp;lt;stdint.h&amp;gt;
#ifdef __cplusplus
extern "C" {
#endif

uint32_t sum(uint32_t a, uint32_t b);

uint32_t square(uint32_t a);

#ifdef __cplusplus
}
#endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;src/example.c&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include "example.h"

uint32_t sum(uint32_t a, uint32_t b)
{
    return a + b;
}

uint32_t square(uint32_t a)
{
    return a*a;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;src/main.c&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

int main(int argc, char** argv)
{
    puts("Hello World!");
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Compilation
&lt;/h2&gt;

&lt;p&gt;To compile the project, follow these steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir build
cd build
cmake ..
cmake --build .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Run the test suite
&lt;/h2&gt;

&lt;p&gt;After compiling, you can run the test suite using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./tests/unit_tests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Upon running the tests, you should see output similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[==========] Running 3 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 2 tests from Example
[ RUN      ] Example.sumTest
[       OK ] Example.sumTest (0 ms)
[ RUN      ] Example.squareTest
[       OK ] Example.squareTest (0 ms)
[----------] 2 tests from Example (0 ms total)

[----------] 1 test from ExampleTest
[ RUN      ] ExampleTest.test
[       OK ] ExampleTest.test (0 ms)
[----------] 1 test from ExampleTest (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 2 test suites ran. (0 ms total)
[  PASSED  ] 3 tests.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see a similar output, congratulations! Your setup is complete and the tests have passed successfully.&lt;/p&gt;

&lt;h2&gt;
  
  
  GitHub Repository
&lt;/h2&gt;

&lt;p&gt;You can find the full project and source code on GitHub:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Yanujz/googletest-boilerplate" rel="noopener noreferrer"&gt;Source code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to clone the repository, explore the code, and contribute if you have any improvements or suggestions.&lt;/p&gt;

&lt;p&gt;I hope this article has saved you some time searching for this information online.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>googletest</category>
      <category>cpp</category>
      <category>cmake</category>
    </item>
    <item>
      <title>Visualize and explain byte sequences with byte-diagram</title>
      <dc:creator>Yanujz</dc:creator>
      <pubDate>Sat, 22 Jun 2024 17:11:24 +0000</pubDate>
      <link>https://dev.to/yanujz/visualize-and-explain-byte-sequences-with-byte-diagram-201e</link>
      <guid>https://dev.to/yanujz/visualize-and-explain-byte-sequences-with-byte-diagram-201e</guid>
      <description>&lt;p&gt;Hey everyone!&lt;/p&gt;

&lt;p&gt;I'm excited to introduce you to a new command-line tool I've been working on called byte-diagram. This tool allows you to visualize byte sequences using ASCII art, making it easier to understand and analyze hexadecimal data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is &lt;code&gt;byte-diagram&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;byte-diagram&lt;/code&gt; is a Python-based command-line utility designed for visualizing byte sequences. It takes a hexadecimal string as input and generates an ASCII diagram that represents each byte and its position in the sequence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;br&gt;
Using byte-diagram is straightforward. You simply provide it with a hexadecimal string as an argument, and it will generate a diagram showing each byte's position and value in a sequential manner. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$./byte-diagram.py -s "00 01 02" -d "Field 0" "Field 1" "Field 2"
   ------------&amp;gt; Field 0
  /     -------&amp;gt; Field 1
 |     /     --&amp;gt; Field 2
 |    |     /     
 |    |    |        
0x00 0x01 0x02 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Where do you find it?&lt;/strong&gt;&lt;br&gt;
You can find &lt;code&gt;byte-diagram&lt;/code&gt; on GitHub &lt;a href="https://github.com/Yanujz/byte-diagram"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contribution&lt;/strong&gt;&lt;br&gt;
Contributions to &lt;code&gt;byte-diagram&lt;/code&gt; are welcome! Whether you want to add new features, improve documentation, or fix bugs, feel free to fork the repository and submit a pull request.&lt;/p&gt;

</description>
      <category>cli</category>
      <category>tools</category>
      <category>asciiart</category>
    </item>
  </channel>
</rss>
