Skip to main content

Posts

Showing posts from July, 2014

Setting up google test and google mock

Google test is a great library that one can use for unit testing. Google mock is an extension of Google test as a mocking framework which can be used to mock the behavior of interfaces. First things first, build the library gmock. I use netbeans. 1. Create a new project  as C/C++ static library 2. Include the files as "Add Existing Item"   ${GTEST_DIR}/src/gtest-all.cc and ${GMOCK_DIR}/src/gmock-all.cc 3. Include the  Directories and Headers for the compiler in project properties. ${GTEST_DIR}/include and ${GMOCK_DIR}/include and  ${GTEST_DIR} and ${GMOCK_DIR} 4. Build..... Note: It's mentioned that you need -pthread but since we are building a static library, the links are not resolved so you need to only enter them in the application linker properties. Now you'll end up with the ".a file" in the dist folder. Now create a new application as a C/C++ project. This project contains your application that should be tested or mocked using Google test f...

Passing a pointer by reference

I wanted to create an array of a certain class but its size was not determined until run time. Therefore I had to of course use a pointer. Now because it was always good practice to initialize a pointer to NULL, I did so and I passed this initialized pointer to a function. This function would then new the pointer inside, seemed OK but this made the program crash. I used the function AllocateArray1 in the gist. Why did it crash? The pointer is passed by value and when the function exits the pointer being passed is not quite initialized. To verify this I used the test case 3 where I created an array, deleted it but didn't assign it back to NULL, this made sure that the pointer is still pointed to a certain memory address. The works because although the memory is deallocated we can still access the values using raw pointers, although this might crash some times due to access violation. The other option of course, which I used was to pass the pointer by reference. AllocateArra...

Math Lesson - Combinations and Permutations

Very often I need to deal with combinations and permutations. This is just a recap on high school math which I tend to lose touch on. A combination doesn't care about the order, e.g. 123 is the same as 312 A permutation does Imagine there are 5 numbers 1,2,3,4,5. We need to choose 3, n=5, r=3. How many combinations? How many permutations? Permutation with repetition n^r Imagine there are 3 slots for the 3 numbers. There's 5 number we can fill the first slot. 5 numbers we can fill the second and 5 number we can fill the third. 5*5*5 permutations. Permutation without repetition n!/(n-r)! There's 5 number we can fill the first slot. 4 numbers we can fill the second and 3 number we can fill the third. 5*4*3 permutations. Combinations without repetition n!/((n-r)!r!) Imagine permutations without repetition. Now remove the sens of permutations Hence an additional r! in the denominator. Combinations with repetition (n+r-1)!/r!(n-1)! This needs an unusual appr...