Page 4 of 6

Unit Testing C# With Data in Visual Studio 2012

When writing unit tests it is likely you will  need to include some additional input files containing data or settings etc. If you’re trying to include some data for a C# unit test and are using Visual Studio 2012 follow this guide –

  1. Add a separate folder for data to the project then ‘add an existing item’ and select your files.
  2. In the Solution Explorer, navigate to your newly added file, right click and go to properties.
  3. Set ‘Copy to Output Directory’ to an appropriate option.
  4. When you want to use this data in a unit test add the following before the function – [DeploymentItem(“Data”, “Data”)]

The line in the last step will copy all data from Data in your build directory to the a new folder in the test directory.

I presume that these instructions will apply to older versions of Visual Studio.

 

Optimising C++ The STL Way

A co worker emailed out an interesting question, how would we optimise the following piece of code –

The code looks fast, light and up to the job right? Sure, it will work fine and it will be reasonably fast, but it can always be faster. The first obvious waste of time is with the sort; sorts are lengthy beasts and as much as possible should be done to minimise their impact. The aim of this function is to get the average of the k smallest values in the array, imagine if the array is ten thousand elements big and you only need the average of the two smallest? Sorting that entire array just to get two values is a waste of time.

So, how do we optimise this? My lecturer at university always used to tell us how good the STL is and that it was nearly always better and faster than anything we can write. The obvious solution is to move away from the primitive programming shown in the above function and see what the STL can do for us; luckily enough there are several great functions that do exactly what we want. The first great time saver is the partial sort which simply sorts the array in such a way that the range we specify contains the sorted elements but anything above that middle point is left unsorted. Simply using this method would save a large amount of time over the original method of doing a full sort.

The only requirement of these improvements is to change the function parameters to accept a vector rather than a float array, luckily there were no rules set in the original question!

In order to show that these changes would be faster I wrote some code to prove it. It runs the old function and the new function ten thousand times with the same data and calculates the time taken based on clock cycles. The quicker the better. Take note that the time also includes creating a copy of the input array. If we just pass in the array it will be sorted in place so in reality we are testing how quick it is on already sorted data.

And the results speak for themselves, across ten thousand iterations of each function the STL function is generally twice as fast.

Original function: (0.0157705) 20.56 sec
STL function: (0.0157705) 7.75 sec

I think I should finally commit to using this site for something. Anytime I find out something useful or create something that has a remote chance of being reused I will stick it on here.

Maybe it could actually live up to the Acodemic name.

Vinaigrette Sort

When programming in C for embedded systems where memory, disk usage and CPU usage are all very strict there are several ‘rules’ in place such as not use dynamically allocated arrays; this ensures that you can retain more control over memory management.

In a recent experience I had a fixed size array which was always populated below capacity, this array needed to be sorted which would normally leave my zero initialised elements at the start of my array and mess up my loop logic further on in the program.

I had an idea that I could use my sort comparison function to do two things – the first is the original sort purpose of ordering my elements, the second would be to move all unused elements to the end of the array.

Guess what, it’s very simple! I present the vinaigrette sort (so called because the oil and vinegar separate into layers in a vinaigrette).

It is as simple as modifying the compare function required by the qsort function. Check if a field in your struct (or your data) is zero and therefore unused and then declare it as ‘greater’ than any comparison. I did find that an older GCC version (3.7) required the line as described in the above source code, GCC 4.7 compiled and ran as required with just the one comparison.

Note the memset line, if you leave the array uninitialised then the compare won’t have anything defined to search for that would signify an unused element.

Python Socketed Receiver

This is a quite cool little class I wrote for receiving log messages over sockets. It is launched as a thread and uses a python Queue to store messages for retrieval by consumers in your main program. It also means I can have multiple sockets for different log sources.

 

Linux Mint on VirtualBox – Enable 3D Acceleration

Just spent a bit of time installing the latest Linux Mint (14.1) on VirtualBox and struggling to get the VirtualBox 3D acceleration to work correctly. This causes the window manager to fall back to software rendering (yuk); to see if your machine is currently using software rendering run inxi -G and check for Gallium 0.4 on llvmpipe (LLVM 0x300).

To get the full 3D acceleration from VirtualBox ensure that 3D acceleration has been enabled from the settings menu and a decent amount of video RAM is available, I have it running quite well with 56MB of RAM. Load up Linux Mint and install the VirtualBox additions from the devices menu. Ignore the warnings about it already being installed and carry on. Reboot the virtual box and run inxi -G again, it should now show something similar to this –

Graphics: Card: InnoTek Systemberatung VirtualBox Graphics Adapter
X.Org: 1.13.0 drivers: ati,vboxvideo (unloaded: fbdev,vesa) Resolution: 1920×1200@60.0hz
GLX Renderer: Chromium GLX Version: 2.1 Chromium 1.9

I only get this problem on a fresh install of Linux Mint. It’s supposed to be bundled with the VirtualBox guest additions but I guess they don’t always work as they should.

WTF Is UPlay?

wtf_is_uplay

Wanted to play Far Cry 3, turns out I have to use yet another pointless game ‘service’. Seriously, for single player I have to register an account and be logged on and all sorts of other rubbish. Really getting tired of this all because of piracy. Next time I’m getting it from the Pirate Bay.

New Gmail Compose Screen

Just been prompted by Gmail to use a new email composing screen. Not a massive change as you’d expect from Google but it does make the web client a bit more seamless. Basically instead of being directed to a new screen when composing an email, a composition window similar to a Google chat window will appear in the lower right hand of your screen.

new gmail compose screen

It is a nice change and will no doubt be rolling out to everyone shortly.

Project Eternity

I have backed my first Kickstarter campaign, and what a place to start! Project Eternity is the idea from a group of clearly very talented developers with titles such as Baldur’s Gate, Fallout and Planescape Torment under their belts. TAKE MY MONEY PLEASE. Cannot wait to see how this game turns out.

Been very impressed with their attitude about the whole thing, very frequent updates and lots of chances to communicate with the developers. I just really hope they preserve the feel of the original Baldur’s Gate which seems to be it’s closest spiritual predecessor.

The Kickstarter process is very easy and clear to use, I will definitely be on the lookout for more good ideas to get behind.

Some more info – http://www.kickstarter.com/projects/obsidian/project-eternity

Today I learnt that it is programmer’s day, apparently we’re all supposed to get beer and pizza? I for one shall be celebrating by porting various tools from Linux to Solaris.

Correct Setup of Python Easy-Install

Starting work on some python driver development today for the ant+ system (more on this later) and had to install a bunch of Python modules that didn’t appear on the standard Ubuntu repos.

Python has a very easy package manager of it’s own called easy tools which is found in the python-setuptools package. But this isn’t everything you need, you’ll receive a lot of errors when trying to use easy tools such as –

msgpack/_msgpack.c:4:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: Setup script exited with error: command ‘gcc’ failed with exit status 1

In order to resolve these errors you’ll need to ensure you have g++ installed as well as the python header and static library files. These two packages are called g++ and python2.7-dev. Change the python version depending on your preference or download the python-all-dev package.

HTC One X – One Hot Potato

I bought the HTC One X after my trusty Desire HD ended it’s life inside my washing machine. A fantastic phone and quite the looker but woah does this thing get hot when it’s doing something!

The other day I was using the Google video chat and the notification LED started flashing orange and green at me, seems to mean that it’s overheating and you should probably stop doing what you’re doing.

There’s a new update doing the rounds at the moment, V2.05 I think, hopefully it will sort some of these problems out.

© 2026 Acodemics

Theme by Anders NorénUp ↑