EspaƱol English
Showing posts with label EN. Show all posts
Showing posts with label EN. Show all posts

Wednesday, 23 December 2015

Blog moved to http://maitesin.github.io/

I decided to move this blog to http://maitesin.github.io/ due a better integration with the tools I am currently using to create this posts.

Tuesday, 22 December 2015

Unit test with Google Test for C++



Introduction

Google Test is one of the available Frameworks to create unit test for C++. In this example I will use CMake to configure the project and build. Furthermore, for the dependency manager I will use the new and shiny conan.
Before starting, why use a dependency manager such as conan or software to configure and build such as CMake? Because these technologies are widely use it in real projects.
All the code and configuration files used for this entry are available in this repo in GitHub.

Step 1 Install conan, configure project and gather dependencies

First of all we need to have install conan from pip2 doing:
pip2 install conan
Now with conan installed we do not have to worry about installing Google Test in our system.
Next step will be preparing the conanfile.txt to gather de dependencies:
[requires]
gtest/1.7.0@lasote/stable

[options]
gtest:shared=True

[generators]
cmake
Once we have conan ready we only need to run it to download the dependency and configure them to have them ready for CMake:
conan install .
This will output something similar to:
WARN: Migration: Updating settings.yml with new gcc versions
Requirements
    gtest/1.7.0@lasote/stable
Package for gtest/1.7.0@lasote/stable in /home/maitesin/.conan/data/gtest/1.7.0/lasote/stable/package/ca89189bc59ff53842d6beea76549f289b7b88bd
Generated conaninfo.txt
Generated conanbuildinfo.cmake

Step 2 configuring CMake

The configuration will be done in the CMakeLists.txt file:
project(Google_test_example)
cmake_minimum_required(VERSION 2.8)

include(conanbuildinfo.cmake)
CONAN_BASIC_SETUP()

INCLUDE_DIRECTORIES(src)
ADD_EXECUTABLE(run_test src/test.cpp)
TARGET_LINK_LIBRARIES(run_test ${CONAN_LIBS})

Step 3 code and unit test

The code will be held in the src folder. It will contain two files: functions.h and test.cpp.
The function(s) we want to test will be in the header file function.h:
int int_addition(int a, int b) {
        int c = a + b;
        return c;
}
The test(s) we want to run will be in the source file test.cpp:
#include "gtest/gtest.h"
#include "functions.h"

TEST(IntAddition, Negative) {
        EXPECT_EQ(-5, int_addition(-2, -3)) << "This will be shown in case it fails";
        EXPECT_EQ(-3, int_addition(5, -8));
}

TEST(IntAddition, Positive) {
        EXPECT_EQ(4, int_addition(1, 3));
        EXPECT_EQ(9, int_addition(4, 5));
}

int main(int argc, char **argv) {
        testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
}

Step 4 putting all together

What is left to do is actually build the project and run the test. In order to do this we need to run:
cmake .
This will output something similar to:
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/maitesin/workspace/blog/google_test_2015_12_22
There will be now a Makefile generated from CMake with everything ready to compile and link all the sources and dependencies together.
make
This will output something like:
Scanning dependencies of target run_test
[ 50%] Building CXX object CMakeFiles/run_test.dir/src/test.cpp.o
[100%] Linking CXX executable bin/run_test
[100%] Built target run_test
This will generate an executable in the bin folder, and we will be able to run them with the command:
./bin/run_test
This will result with the following output:
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from IntAddition
[ RUN      ] IntAddition.Negative
[       OK ] IntAddition.Negative (0 ms)
[ RUN      ] IntAddition.Positive
[       OK ] IntAddition.Positive (0 ms)
[----------] 2 tests from IntAddition (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[  PASSED  ] 2 tests.

Tuesday, 10 November 2015

Tmux, basic command and usage



Tmux, basic commands and usage

How to install tmux in Archlinux/Manjaro

To install tmux in Archlinux/Manjaro you only need to type in the terminal:
yaourt -S tmux

How to start using tmux

Opening a tmux session

Tmux works with sessions, if you know screen you are familiar with this kind o sessions. How to create a session:
tmux new -s main
What does this command? It creates a session (-s) with a label called main. Label are useful if you have several sessions running.

Detaching from a tmux session

Detaching from a session means that tmux will keep running this session (as well as the programs running on it), but you will not see it. Really useful to keep it running in a server with something you do not want to loose even if the connection fails. To detach from a session simply type control + b and afterwards d

Attach to a tmux session

Attaching to a session means that we will join again to a session we detached before. To do that you simply type:
tmux attach -t main
What does this command? It attaches to (-t) to a session with label main.

Demo of creating, detaching and attaching to a session


Inside tmux, basic steps

Split window horizontal

To split horizontally the window we only need to press: control + b and

Split window vertical

To split vertically the window we only need to press: control + b and %

Moving between windows

To move between windows we only need to press: control + b and an arrow key one time.

Resizing windows

To resize a window we only need to press: control + b and arrow key several times.

Demo of splitting, moving and resizing windows


Create a new tab in the session

To create a new tab and move to it we only need to press: control + b and c. Check in the bottom of the terminal that you can see how many tabs do you have and what do they have focused.

Go to next tab

To go to the next tab we only need to press: control + b and n

Go to previous tab

To go to the previous tab we only need to press: control + b and p

Demo of creating and swapping tabs


Well, that was the basics. You can do plenty more in tmux, but for that you will have to use change the config file. As always, if people wants to know more about it and how to configure it, please leave a comment.

Saturday, 17 October 2015

How to install Xmonad in Arch Linux/Manjaro (2/2)

Now it is time to talk about my personal configuration in Xmonad. I will show pieces of the configuration file and I will explain what they mean (no worries, we will not go into Haskell details) and I will show some pictures if needed.
   
myTerminal      = "xterm"

myWorkspaces  = ["1:Main","2:Xat","3:Web","4:Music","5:Video","6", "7", "8", "9"]  

myModMask       = mod4Mask
  
In this code above we can see that xterm will be my terminal, my 9 workspaces (note that the names are strings, then you can change them as you wish) and the modMask button (using the windows button, mod4Mask, to use Xmonad actions).

myLayout = onWorkspace "2:Xat" pidginLayout $ onWorkspace "5:Video" nobordersLayout $ tiled1 ||| Mirror tiled1 ||| nobordersLayout  
 where  
  tiled1 = spacing 5 $ Tall nmaster1 delta ratio  
  --tiled2 = spacing 5 $ Tall nmaster2 delta ratio  
  nmaster1 = 1  
  nmaster2 = 2  
  ratio = 2/3  
  delta = 3/100  
  nobordersLayout = smartBorders $ Full  
  gridLayout = spacing 8 $ Grid       
  --gimpLayout = withIM (0.20) (Role "gimp-toolbox") $ reflectHoriz $ withIM (0.20) (Role "gimp-dock") Full  
  pidginLayout = withIM (18/100) (Role "buddy_list") gridLayout

In this part of the configuration file I am specifying what the behaviours of the different workspaces are. We can see that workspace 2:Xat will be using pidginLayout and workspace 5:Video will be using nobordersLayout. All the other workspaces will have three layouts (you can change them as you wish).

After that, we can see for tiled1 the ratio of the amount of screen we want to use between the master window and the stack. Moreover, we can see how the pidginLayout and nobordersLayout are specified. Please, see below a screenshot for the pidginLayout.

Pidgin running with special layout in workspace 2:Xat

myManageHook = composeAll       
     [ className =? "File Operation Progress"   --> doFloat  
     , resource =? "desktop_window" --> doIgnore  
     , className =? "xfce4-notifyd" --> doIgnore  
     , className =? "Pidgin" --> doShift "2:Xat"  
     , title =? "New Tab - Google Chrome" --> doShift "3:Web"
     , className =? "Firefox" --> doShift "3:Web"
     , className =? "Clementine" --> doShift "4:Music"  
     , className =? "Vlc" --> doShift "5:Video"  
     ]  


In this piece of configuration file we can see the hooks that we want to have in the system. I think they are quite straight forward, but I would like to mention that Firefox is moved always to workspace 3:Web or Vlc is moved to workspace 5:Video.

    ------------------ 
    -- Window Managing
    ------------------
    -- Move focus to the next window    
    , ((mod4Mask, xK_j), windows W.focusDown)
    -- Move focus to the previous window
    , ((mod4Mask, xK_k), windows W.focusUp)
    -- Move focus to the master window
    , ((mod4Mask, xK_m), windows W.focusMaster)
    -- Swap the focused window and the master window
    , ((mod4Mask, xK_Return), windows W.swapMaster)
    -- Swap the focused windows with the next window
    , ((mod4Mask .|. shiftMask, xK_j), windows W.swapDown)
    -- Swap the focused windows with the previos window
    , ((mod4Mask .|. shiftMask, xK_k), windows W.swapUp)
    -- Shrink the master area
    , ((mod4Mask, xK_h), sendMessage Shrink)
    -- Expand the master area
    , ((mod4Mask, xK_l), sendMessage Expand)

This is another important piece of the configuration file. This snippet shows how to interact with the windows you have in a workspace. Let's read a few lines to see how it works. The first one is how to focus on the next window, for that we have to use the combination Win+j or to move to the previous window we have to use the combination Win+k.

    -------
    -- Apps
    -------
    -- Google Chrome
    , ((mod4Mask, xK_g), spawn "google-chrome-stable")
    -- Pidgin
    , ((mod4Mask .|. shiftMask, xK_p), spawn "pidgin")
    -- VirtualBox
    , ((mod4Mask, xK_v), spawn "VirtualBox")
    -- Clementine
    , ((mod4Mask, xK_c), spawn "clementine")
    -- VLC
    , ((mod4Mask .|. shiftMask, xK_v ), spawn "vlc")
    -- Terminator
    , ((mod4Mask, xK_Return), spawn "terminator") -- spawn terminator terminal  
    -- Thunar
    , ((mod4Mask, xK_t), spawn "thunar")  
    -- Dmenu
    , ((mod4Mask, xK_p), spawn "dmenu_run")
    -- Kill the focused app
    , ((mod4Mask .|. shiftMask, xK_c), kill) -- kill the app

The last piece of configuration file shows how to launch applications using shortcuts. As you can see, they are as easy as the movements of the windows. To launch pidgin I will have to use Win+Shift+p or to kill the app which I am focused I will use Win+Shift+c.

Finally, to apply these changes you need to compile Xmonad and restart the session. To compile your changes you only have to write in the terminal:

xmonad --recompile


That's all for today. If anyone has questions or wants more information, please, feel free to write a comment.

Monday, 12 October 2015

How to install Xmonad in Arch Linux/Manjaro (1/2)

In this post I will talk about how to install Xmonad and use my personal configuration files to make it run. Remember the videos are made with asciinema. That means you can copy from the video!!

First step is adding archlinuxfr repo to the /etc/pacman.conf file and installing yaourt.

Install xmonad, xmobar, dmenu, zsh, git, feh and all other needed stuff.

Gather dot-files from my repository in GitHub and enable SLiM as login manager.

Once you log into the system you should see something like:

There are plenty of configuration files put in place to make it run. Therefore, it will be so long to explain each of them, but if people are interested in some or all of them I will make another entry explaining them.

In the next post I will talk about the configuration I have in Xmonad and how to use it. If you want to see something in advance have a look to the file ~/.xmonad/xmonad.hs

Finally, recently I have been playing with DWM, another tiling window manager, from suckless.org. I will make another entry in the future explaining about it, because I think is really interesting.

Thursday, 27 August 2015

Installing Manjaro minimal

Manjaro has different flavours and each one has their own installer, but there is a minimal version of Manjaro. The minimal version only installs the required software to start the system. At the end we have something similar such after installing Arch Linux, but we all advantages of Manjaro.

Manjaro minimal net install 64 bits

Manjaro minimal net install 32 bits

Right below you can watch a video where I show how you can install Manjaro minimal (for those ones who remember the old Arch Linux installer this will remind you to it)

NOTE: When it looks like I am doing nothing in the /etc/hostname file actually is installing the system, but it is not shown in the video.

I hope you liked it. In the next post I will explain how to install and configure Xmonad in Manjaro.