Intro to Mocking with Moq

It’s easy to overlook the importance of unit testing. Writing tests can be tedious. They must be updated constantly as code is refactored, and when you have a large code base, you may have to write many tests to even come close to testing all cases. Despite this, unit testing is a necessary part of creating clean, working code. One way to make the testing process easier is through the use of mocks. Read more on Intro to Mocking with Moq…

Google Protocol Buffers Libraries for C

Google Protocol Buffers have become a very popular serialization mechanism for simple to complex/variant data structures. Due to the variation of message formats in a given protocol, the presence and/or lack of fields on an ad-hoc basis makes implementation in the C language somewhat of a challenge.

Three Protocol Buffers Libraries for C

Thanks to the open-source world, a couple of valid options exist, but choosing the right one depends on the complexity of the protocol and memory/performance requirements.

Google Protocol Buffers for C++

The obvious first choice may seem to go right to the source. Google’s Protocol Buffer Compiler supports the generation of a C++ protocol implementation from a .proto specification. Though you are developing in C, and your complier supports C++, you could use the C++ API directly from C, or at least create a C wrapper for the target protocol.

  • Pros: Formally supported by Google, bleeding edge
  • Cons: Well, it’s C++ and does lots of dynamic memory allocation

Read more on Google Protocol Buffers Libraries for C…

Nicer C99 APIs with Designated Initializers

While working on a library for property-based testing in C, I discovered a trick that can be used to make significantly nicer library interfaces in C99: “designated initializers”. As of C99, struct literals can have their fields set by name, in any order. The C99 standard explicitly updated the behavior for how fields in struct literals are handled:

6.7.8 point 21:

“If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.” (emphasis mine)

Since memory with static storage duration is already initialized to zero, this means that in C99 we can finally depend on stack-allocated structs’ fields being set to zero, rather than garbage data from previous stack frames. This is a huge improvement! If pointers to structs are used as arguments for function calls, it also gives C99 a portable way of using optional and keyword arguments. Read more on Nicer C99 APIs with Designated Initializers…

theft: Property-Based Testing for C

Recently, I discovered a bug in my heatshrink data compression library, a result of a hidden assumption — I expected that input to uncompress would be padded with ‘0’ bits (like its compressed output), but if given trailing ‘1’ bits, it could get stuck: it detected that processing was incomplete, but polling for more output made no further progress.

Read more on theft: Property-Based Testing for C…

Talking SPI on Raspberry Pi

The Serial Peripheral Interface (SPI) Bus is used for communication between microcontrollers and other digital devices. On the Raspberry Pi it can be very handy for doing things like acquiring values from an Analog-to-Digital converter, reading a temperature sensor, or communicating with another microcontroller.

Read more on Talking SPI on Raspberry Pi…

Undefined Behaviors in C that You’ve Probably Run into without Knowing It

In many languages, when you are unsure of a particular detail of the language, you can often “just run it” and see what happens. This might work in another language, but in C this will almost certainly bite you. It’s too easy to to accidentally invoke “undefined behavior”, where your code might do one thing in one case, but something totally different in another, without even getting a warning from the compiler.

Here are a few undefined behaviors you might not know about, along with the relevant section from the C99 spec. These aren’t just pedantic ramblings; they’re all cases that I’ve encountered on real projects out in the wild. Read more on Undefined Behaviors in C that You’ve Probably Run into without Knowing It…

Functional(ish) C# & MVVM: Single-Responsibility and Code-as-Data

I’ve spent the last year working in C# and WPF. Over a few blog posts, I have alluded to a particular pattern of structuring code, referring to it as “stateless single responsibility” objects or “stateless SRP” objects. For the purposes of this post I will call the pattern “stateless single responsibility principle” (SSRP) objects. I’d like to go into a bit more detail about what the pattern is and why I use it.

I think it will be simplest to begin with an example problem we might use my pattern to solve, then show how I would do it and explain all the parts. Read more on Functional(ish) C# & MVVM: Single-Responsibility and Code-as-Data…

2 Ways to Use Single-Member structs in C

I love C’s structs. There’s a lot of weirdness in C but, for the most part, structs are predictable, useful, and easy to understand.

Structs, for those not familiar with C, are collections of data. An example of a struct is a point on a Cartesian plane:

struct point {
  int x;
  int y;

Normally, structs are used to associate two or more pieces of data. As the title suggests, I’m going to demonstrate why you might want to use a struct with a single element. Read more on 2 Ways to Use Single-Member structs in C…