Biicode-logo
  • Explore
  • Plans
  • Learn
    • Get Started
    • Downloads
    • Docs
    • Blog
    • Forum
  • Log in
C/C++ Arduino Raspberry Pi (cross compiling) Node.js
  • Biicode
    • Installation
      • Install Biicode
      • Install C/C++ tools
      • Debian based distributions
      • Arch based distributions
      • Run biicode from source
      • Install C/C++ tools manually
        • Verify your installation
      • Connect through a proxy server
    • Getting started
      • Basics
      • Create your first project
        • [optional] Keeping #includes short
      • Using an IDE
      • Build and run
      • Publishing
    • Dependencies
      • Dependencies
      • Modifying the version you depend on
        • Depending on a block track
    • Publishing
      • Tag a version
      • Private blocks
    • Custom build configuration
      • CMake basics
      • Where is biicode´s “magic”?
      • Define and prepare targets
        • Which source code files are part of the block’s library
        • Choose STATIC or SHARED library
        • Modify which executable targets are made
        • Which source code files are part of each executable
        • Modify which test targets are made
      • Configure targets
        • Configure library target
        • Configure executable target
      • Select build type: Debug or Release
      • Complete variable reference
    • Adapt your library
      • Concepts to understand
        • Key facts
      • Without a previous CMakeLists.txt
        • 1. Look for unresolved dependencies with bii deps
        • 2. Execute bii build
        • 3. Test the libary’s reusability
      • With a previous CMakeLists.txt
        • Option 1: Let biicode do its job in an isolated file
        • Option 2: Build your own target library and link them to BII_LIB_TARGET
        • Option 3: Adapt your CMakeLists.txt filtering files
    • Advanced Usage
      • Custom Layouts
        • Simple Layout
        • TMP Layout
        • Classic Layout
        • CLion Layout
      • Tests
      • Open multiple blocks
        • Working with your own blocks
        • Opening your block
        • Publishing updated code
        • Closing edited block
        • Working with any published block
        • Open a block
        • Publish the changes
        • Close the block
        • Depend on the block you’ve just published
      • Toolchains
      • Override a dependency
        • Override a dependency with block tracks
      • Advanced build configuration
        • Publish, share and reuse CMake scripts
        • Overriding dependencies build options and configuration
      • Publish a block track
        • Publish a new block Track
      • Private blocks
    • Examples
      • Basic Compression Library
        • Simple Huffman Compression - Uncompression
        • Open and build
      • Boost Libraries
        • Boost.Lambda
        • Boost.Coroutine
        • Alternative setup call
        • Extra configuration variables
        • Contribute to the setup scripts
      • Box2D
        • Bounces of a circle falling
        • Open and build
      • C++ challenge
        • Create a new project
        • Copy the code
        • Find and retrieve dependencies
        • Build and run
      • CImg
        • Tron game
        • Open and build
      • Crypto++
        • Encrypt a message
        • Open and build
      • CSparse
        • Read a matrix and solve a linear system
        • Create a new project
        • Open and build
      • cURL
        • HTML page gatherer
        • Open and build
      • Eigen
        • Middle rows from a matrix
        • Open and build
      • Expression Parser
        • Simple form of mathematical expression parsing
        • Open and build
      • fit
        • Tests
      • Flatbuffers
        • Charge a *.fbs file and generate a C++ header
      • Freeglut
        • 1. Create a new project
        • 2. Creating reusable code
        • 3. Find dependencies
        • 4. Build and run
      • GLFW
        • Running the examples
      • GLUI User Interface Library
        • GLUI Window Template
      • Google Mock (GMock)
        • GMock Examples
        • Mocking a simple function
      • Google Test (GTest)
        • Testing a factorial function: Simple test
        • Testing a factorial function: Test suites
        • Open and build
      • HTTP Server
        • How does it work?
        • How can I use it?
        • The code
        • Supported Operating Systems
        • More information
        • List of dependencies
      • json11
        • Simple convert data to json and vice versa
      • json++
        • Simple parser and converter from JSON to XML
      • Miniutf
      • Multivariate Splines
      • libuv
        • Http client/server application
        • Change libuv’s version
      • Little CMS
        • ICC Profile Examples
        • Open and build
      • Log4z
        • Fast stream log strings test
      • lwan Web Server
        • Hello World Example
      • MiLi
      • MuParser
      • OpenCV
        • Showing an image and detecting faces
        • Open and build
      • OpenSSL
        • Encrypting with MD5 and SHA1
        • Develop your project
        • Open and build
      • POCO
        • PDF example
        • Using NetSSL_OpenSSL or NetSSL_Win library
      • PTypes
      • SDL
        • Graphical window interface
        • Open and build
      • SQLite
        • Shopping list database
        • SQLite++ Wrapper
        • Open and build
      • TinyThread++
        • Open and build the examples
        • Simple Hello World with a thread
        • Draw a fractal
      • Zlib
        • Usage example (difficulty: medium)
      • ZMQ
        • Simple client-server with C++ binding
      • ZMQ with Google Protocol Buffers Serialization
    • Integrations
      • Generators and IDEs
        • Eclipse CDT
        • Visual Studio
        • CLion
      • IDEs and VCS
      • Git (GitHub, Bitbucket, etc.)
        • With a new repository
        • Create a block from a git repository
        • Publish from git commit
      • Continuous Integration
        • AppVeyor
        • Travis CI
      • Koding
      • Doxygen
        • Create a Doxyfile template
        • Edit your Doxyfile
        • Generate the Documentation
    • Reference
      • biicode.conf: configure your biicode projects
        • [requirements]
        • [parent]
        • [paths]
        • [dependencies]
        • [mains]
        • [tests]
        • [hooks]
        • [includes]
        • [data]
      • Commands
        • bii build: build your project
        • bii buzz: init, find and build
        • bii clean: delete meta-information
        • bii close: finish editing published blocks
        • bii configure: configure your project
        • bii deps: show block dependencies
        • bii diff: compare block versions
        • bii find: find your external dependencies
        • bii init: creates a new project
        • bii new: creates new blocks
        • bii open: edit published blocks
        • bii publish: publish your blocks
        • bii setup: install necessary tools
        • bii test: test your code
        • bii update: update a block
        • bii user: specify your username
      • Configuration Files
        • layout.bii: define your project layout
        • policies.bii: defining the policies for the code you want to reuse
        • ignore.bii: filtering your files
        • settings.bii: defining your tools and preferences
        • types.bii: configuring non-standard file extensions
    • Release notes
    • FAQs
      • Is biicode free?
      • Is biicode an editor in the cloud?
      • Is biicode a VCS?
      • Can I use biicode with my favourite VCS?
      • Which languages are supported?
      • How does biicode relate to Maven, NPM, PyPI...?
    • Troubleshooting
      • Eclipse projects: “Launch failed. Binary not found” (OS X)
      • g++ doesn’t compile simple code, using thread header
      • Default Build Configuration with bii build not working
  • Arduino
    • Installation
      • Install Biicode
      • Install Arduino tools
      • Install Arduino tools manually
        • Install Arduino SDK manually
    • Getting started
      • Installing biicode and Arduino tools
      • Create your project
      • Define your board
      • Build and upload your program
      • Depending on Fenix Blink
      • Build and upload
    • Arduino commands
      • bii configure -t arduino: configure your project
      • bii build: build your project
      • bii arduino:upload: send your code into the Arduino
      • bii arduino:settings: configure your Arduino settings
      • bii arduino:monitor: start a serial monitor
      • bii configure –toolchain=arduino: enable, disable or change the Arduino cross compilation
    • How to
      • Eclipse IDE configuration
        • How to import your project
        • How to fix “Unresolved inclusion: Arduino.h”
      • Configure your SDK, port and board
        • I changed my Arduino’s port, what happens now?
        • How can I change my Arduino project properties?
        • bii arduino:settings options
      • How to adapt your code
        • 1. Projects with one single .ino file
        • 2. Projects with multiple .ino files
      • How to use the Arduino Yun
        • Download Arduino 1.5
        • Configure your settings
    • Examples
      • Arduino Serial Monitor
        • C++ code
        • Turn ON/OFF one LED
      • Servo and LCD 2x16
        • What do we need?
        • Scheme
        • The code: Display the servo angle into a LCD
        • Build and upload the code
      • Arduino Serial Interface
        • How does it work?
        • How do I use it?
        • C++ code
        • Arduino code
        • Build and run!
    • Troubleshooting
      • Launching Arduino IDE, I get an error ./arduino: 22: ./arduino: java: not found in Ubuntu
  • Raspberry Pi Cross Compilation
    • Installation
      • Install Biicode
      • Install RPI tools
      • Install RPI cross-compiling tools manually
        • C++ tools installation
        • Raspberry Pi tools installation
    • Getting started
      • 1. Installing biicode and C/C++ cross-building tools
      • 2. Create your project
      • 3. Build and run your program (cross-compiling)
      • 4. Send your executable to your Raspberry Pi
      • 5. Depending on WiringPi
    • RPi commands
      • bii rpi:send: send a bin folder
      • bii rpi:settings: configure your Raspberry Pi settings
      • bii rpi:ssh: connect by ssh with the Raspberry Pi
      • bii configure –toolchain=rpi: enable, disable or change the Raspberry Pi cross compilation.
    • How to
      • Installing the biicode package from downloads page is too slow
      • Output selection and volume control
      • Raspberry Pi GPIO Pin Layout
    • Examples
      • WiringPi: C GPIO library
        • How to make a LED blink with Raspberry Pi
        • How to use the RPi PWM output to control a motor
        • How to use softServo to control a Servo
      • HTTP Server: how to control a led by web
        • How can i use it?
      • A funny moving doll with Raspberry PI and biicode
        • You just need paper, scissors, a servo, a Raspberry Pi and biicode!
        • Choose the paper doll you like most
        • Putting it all together!
        • Stick the head to the servo and put the servo in the body
        • Connect the servo to the 5v, GPIO17 and 0v pins
    • Troubleshooting
      • Is it possible to change the version of gcc used for cross-compiling to the Raspberry Pi?
  • Node.js
    • Getting started
      • 1. Installing biicode and node.js
      • 2. Create your project
      • 3. Run your program
      • 4. Depending on redis
    • How to
      • Run your node programs
Read the Docs v: latest
Versions
latest
stable
Downloads
pdf
htmlzip
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.
  • biicode docs
  • Biicode
  • Examples
  • Google Test (GTest)
  • Edit on GitHub
Google Mock (GMock)
HTTP Server

Google Test (GTest)¶

Google C++ Testing Framework is a C++ library for testing your projects.

The main block is at google/gtest and it is generated from this github repo.

You can check all the gtest examples which are uploaded in biicode and execute any of them.

Testing a factorial function: Simple test¶

There are two examples to build and execute, a simple test and a test suites. In this part, you’ll run a simple test for a simple “math example code”.

Creating a new project¶

Create a new project and math_ext.h, math_ext.cpp files:

$ bii init gtest_example -L
$ cd gtest_example
$ # Create files manually

Our math example is a simple function to calculate the factorial of an integer number, which returns

  • -1 if the number is negative,
  • 1 if the number is zero, or
  • The factorial if the number is positive: (num)*(num-1)*(num-2)*…*1.

Copy the code for each file:

math_ext.h

#pragma once
//Function returns the factorial of an integer number
int Factorial (int num);

math_ext.cpp

#include "math_ext.h"
int Factorial (int num) {
  if (!num)return 1;
  if (num<0)return -1;
  return num*Factorial(num-1);
}

Okay, now create a new directory simple_test with simple_test.cpp file and copy its content:

simple_test/simple_test.cpp

#include "../math_ext.h"
#include "gtest/gtest.h"
#include "stdio.h"
// Tests Factorial()
// Tests factorial of negative numbers.
TEST(FactorialTest, Negative) {
        // This test is named "Negative", and belongs to the "FactorialTest"
        // test case
        EXPECT_EQ(-1, Factorial(-5));
        EXPECT_EQ(-1, Factorial(-1));
        EXPECT_LT(Factorial(-10), 0);
}
// Tests factorial of 0.
TEST(FactorialTest, Zero) {
        EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
        EXPECT_EQ(1, Factorial(1));
        EXPECT_EQ(2, Factorial(2));
        EXPECT_EQ(6, Factorial(3));
        EXPECT_EQ(40320, Factorial(8));
}
int main(int argc, char **argv) {
  printf("Running main() from sample1\n");
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

Configure biicode.conf¶

Check the dependencies of the project with bii deps:

$ bii deps
INFO: Processing changes...
your_user/gtest_example depends on:
      system:
         stdio.h
      unresolved:
         gtest/gtest.h

Now, edit the biicode.conf file generated in the project folder. Add your [requirements] depending on the version you want and map your [includes]:

biicode.conf

[requirements]
    google/gtest: 10

[includes]
    gtest/*.h: google/gtest/include

Type bii deps again to check all dependencies are resolved.

To configure the simple_test.cpp you have to include it in [tests] section like this:

biicode.conf

[tests]
    simple_test/simple_test.cpp

Build and run the test¶

Compile and execute the test, the convenient command for that is bii test:

$ bii test
 INFO: Processing changes...
 INFO: Saving files from: google/gtest
 ...
test 1
    Start 1: examples_gtest_example_simple_test_simple_test
1: Running main() from sample1
1: [==========] Running 3 tests from 1 test case.
1: [----------] Global test environment set-up.
1: [----------] 3 tests from FactorialTest
1: [ RUN      ] FactorialTest.Negative
1: [       OK ] FactorialTest.Negative (0 ms)
1: [ RUN      ] FactorialTest.Zero
1: [       OK ] FactorialTest.Zero (0 ms)
1: [ RUN      ] FactorialTest.Positive
1: [       OK ] FactorialTest.Positive (0 ms)
1: [----------] 3 tests from FactorialTest (0 ms total)
1:
1: [----------] Global test environment tear-down
1: [==========] 3 tests from 1 test case ran. (1 ms total)
1: [  PASSED  ] 3 tests.
1/1 Test #1: examples_gtest_example_simple_test_simple_test ...   Passed    0.04 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.06 sec
[100%] Built target check

Great! Your math code passed the simple test.

Testing a factorial function: Test suites¶

To have multiple test classes and run them all together you don’t need to create multiple main methods, you just have to indicate which tests you want to include in your suite.

For example, you can split the simple_test.cpp in the following three files.

Place these three files in a new directory called test_suites inside your project.

test_suites/test_factorial1.cpp

#include "../math_ext.h"
#include "gtest/gtest.h"
// Tests Factorial()
// Tests factorial of negative numbers.
TEST(FactorialTest, Negative) {
        // This test is named "Negative", and belongs to the "FactorialTest"
        // test case
        EXPECT_EQ(-1, Factorial(-5));
        EXPECT_EQ(-1, Factorial(-1));
        EXPECT_LT(Factorial(-10), 0);
}
// Tests factorial of 0.
TEST(FactorialTest, Zero) {
        EXPECT_EQ(1, Factorial(0));
}

test_suites/test_factorial2.cpp

#include "../math_ext.h"
#include "gtest/gtest.h"
#include "stdio.h"
// Tests Factorial()
// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
        EXPECT_EQ(1, Factorial(1));
        EXPECT_EQ(2, Factorial(2));
        EXPECT_EQ(6, Factorial(3));
        EXPECT_EQ(40320, Factorial(8));
}

test_suites/test_suites.cpp

#include "gtest/gtest.h"
#include "stdio.h"
// Tests Factorial()
int main(int argc, char **argv) {
  printf("Running main() from sample1\n");
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

Configure biicode.conf¶

You have to indicate in the [dependencies] section in your biicode.conf file that the main file test_suites.cpp depends on those test files: test_factorial1.cpp and test_factorial2.cpp.

Read more about [dependencies] section here.

And you have to indicate the new test in the [tests] section.

Edit your biicode.conf like this:

biicode.conf

[dependencies]
        test_suites/test_suites.cpp + test_suites/test_factorial1.cpp
        test_suites/test_suites.cpp + test_suites/test_factorial2.cpp

[tests]
    simple_test/simple_test.cpp
    test_suites/test_suites.cpp

Build and run the tests¶

Now execute your tests and you’ll obtain the following output:

$ bii test
INFO: Processing changes...
INFO: Saving files from: google/gtest
...
test 1
    Start 1: examples_gtest_example_simple_test_simple_test
1: Running main() from sample1
1: [==========] Running 3 tests from 1 test case.
1: [----------] Global test environment set-up.
1: [----------] 3 tests from FactorialTest
1: [ RUN      ] FactorialTest.Negative
1: [       OK ] FactorialTest.Negative (0 ms)
1: [ RUN      ] FactorialTest.Zero
1: [       OK ] FactorialTest.Zero (0 ms)
1: [ RUN      ] FactorialTest.Positive
1: [       OK ] FactorialTest.Positive (0 ms)
1: [----------] 3 tests from FactorialTest (0 ms total)
1:
1: [----------] Global test environment tear-down
1: [==========] 3 tests from 1 test case ran. (0 ms total)
1: [  PASSED  ] 3 tests.
1/2 Test #1: examples_gtest_example_simple_test_simple_test ...   Passed    0.04 sec

test 2
    Start 2: examples_gtest_example_test_suites_test_suites
2: Running main() from sample1
2: [==========] Running 3 tests from 1 test case.
2: [----------] Global test environment set-up.
2: [----------] 3 tests from FactorialTest
2: [ RUN      ] FactorialTest.Positive
2: [       OK ] FactorialTest.Positive (0 ms)
2: [ RUN      ] FactorialTest.Negative
2: [       OK ] FactorialTest.Negative (0 ms)
2: [ RUN      ] FactorialTest.Zero
2: [       OK ] FactorialTest.Zero (0 ms)
2: [----------] 3 tests from FactorialTest (0 ms total)
2:
2: [----------] Global test environment tear-down
2: [==========] 3 tests from 1 test case ran. (0 ms total)
2: [  PASSED  ] 3 tests.
2/2 Test #2: examples_gtest_example_test_suites_test_suites ...   Passed    0.03 sec

100% tests passed, 0 tests failed out of 2

Total Test time (real) =   0.09 sec
[100%] Built target check

Congrats! Your math code passed both “simple_test” and “test_suites”.

You can aggregate as many tests as you want to a suite so you can organize your tests to fit your needs.

Note: You can find more google test samples in the google/gtestsamples block.

Open and build¶

This example is already in biicode: examples/gtest.

To give it a try and see how it’s configured, create a new project and open the block:

$ bii init gtest_example
$ cd gtest_example
$ bii open examples/gtest

Have a look at the biicode.conf file:

biicode.conf

[requirements]
         google/gtest: 10

[parent]
        examples/gtest: 1
[paths]

[dependencies]
        test_suites/test_suites.cpp + test_suites/test_factorial1.cpp
        test_suites/test_suites.cpp + test_suites/test_factorial2.cpp

[mains]

[hooks]

[includes]
    gtest/*.h: google/gtest/include

[data]

[tests]
    simple_test/simple_test.cpp
    test_suites/test_suites.cpp
Read more about biicode.conf.

Now execute the tests:

$ bii test
...
100% tests passed, 0 tests failed out of 2

       Total Test time (real) =   0.09 sec
       [100%] Built target check

Got any doubts? Do not hesitate to contact us, visit our forum and feel free to ask any questions.

Google Mock (GMock)
HTTP Server

© Copyright 2014, biicode.