CategoryProgramming

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

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.

 

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.

Language Detection Algorithm

My thesis required dealing with large quantities of Twitter data and to make it easier for myself I decided to only use English language tweets. Due to the ‘interesting’ grammar and spelling that is frequently found on line a different algorithm to a standard dictionary test was required. This one checks for common English n-grams (letter groupings) and returns a value of how sure the input is English.

Here’s some examples:

Good morning everyone! : 66%
Im So Sick. Really in a bad Position : 50%
Thats the fear of unicorns : 100%
Gnt too com uma saudade daminha namorada vcs nao tem ideia :(((( : 0%
Ils testent les hologrammes pour faire des concerts par des morts. Bientôt même les pas morts feront des hologrammes et vous irez les voir. : 12%
il7ain ilnass ykhl9un men habat twitter o yntqlun to istagram=))!!! : 40%

 

Python Feed Forward Perceptron

I thought I would post a couple of useful bits of code from my final year of university.

The first is a feed forward perceptron written in Python. I tried to expand the examples on the internet as much as possible to help me understand how they work.

Pointless Error Messages

Gee, thanks Battlefield

So you’re sat there playing a game, about to pull off some spectacular moves and own some noobs when your game freezes, you fall through the floor and you are dumped back into the reality of the Windows Desktop. Quietly muttering some choice words about computers you discover a fantastic error message, informing you that ‘something went wrong’… Well, thanks error message, without your thoughtful take on everything I would have presumed to have teleported into a map that looks suspiciously like my desktop!

These error messages are becoming more and more common, and yes I fully understand that the alternative is the blue screen of death, which is equally unhelpful, but if you’re going to go to the effort of handling errors correctly without breaking everything why waste all that effort by just telling the user that something went wrong. How about, something went wrong and here is what it was?

Because I want a browser with sass

If it’s that important to try and hide the gory details of an error from the average user have a nice button called technical details. Average Joe isn’t going to click that, he still thinks an illegal operation involves the police.

How about some form of standard for displaying error messages? Maybe code red, yellow and green? Easy to understand for everyone.

Got any more examples of useless error messages?


Free Twitter Data Set

I needed a twitter data set for my thesis and struggled to find one that was freely available. I ended up downloading the data I needed so I thought I would release it here.  It does come in a slightly annoying format which is a MongoDB dump file but it should be easy enough to extract to there and use it. The structure of each record is shown below.

So if you wanted to go over all records in the database you would do it like this –

Download Twitter Mongo Dump

CryENGINE 3

The latest version of the Crytek engine has been released, yet again pushing what you think is possible on current hardware. Another good bit of information is that Crysis 3 will be out next year. Unfortunately that means it will be released on the Xbox and the PS3 which seriously hampered the graphic performance in Crysis 2. Now if they were to focus on a PC only version we could really see some of the magic that the original Crysis brought to it. Fun fact, the original Crysis was being used as a benchmark in Custom PC magazine three years after it was released.

Check out the video here –

How awesome would it be to see Skyrim using this engine?

Code Golf Progress Bar

Had a go at some code golf today, went for this challenge: http://codegolf.stackexchange.com/questions/5382/create-an-ascii-progress-bar

Here is my solution which is 125 characters small.

So the output for this would be as such:

© 2026 Acodemics

Theme by Anders NorénUp ↑