Skip to content

Instantly share code, notes, and snippets.

@kevincox
Last active December 25, 2015 22:18
Show Gist options
  • Save kevincox/7048204 to your computer and use it in GitHub Desktop.
Save kevincox/7048204 to your computer and use it in GitHub Desktop.
Checking for compatible version.
#include <assert.h>
/// Check if versions are compatible.
/**
* Compares the required and available versions and returns true iff the
* requirement is satisfied.
*/
int compatible(unsigned r_major, unsigned r_minor, unsigned r_patch,
unsigned a_major, unsigned a_minor, unsigned a_patch)
{
if ( r_major == 0 )
{
return r_major == a_major && // For version 0 there are no compatibility
r_minor == a_minor && // guarantees, so if the versions are different
r_patch == a_patch ; // we must assume they are not compatible.
}
if ( r_major != a_major ) return 0; // Available version is not backwards compatible.
if ( r_minor > a_minor ) return 0; // Needs features not in available version.
// Don't compare minor versions, they are not relevant.
return 1;
}
int main (int argc, char **argv)
{
assert(compatible(1,2,3, 1,2,3));
assert(compatible(0,2,3, 0,2,3));
assert(compatible(1,2,1, 1,2,3));
assert(compatible(1,2,5, 1,2,3));
assert(compatible(1,2,3, 1,5,2));
assert(!compatible(0,2,4, 0,2,3));
assert(!compatible(0,3,3, 0,2,3));
assert(!compatible(1,3,3, 1,2,3));
assert(!compatible(1,2,3, 2,2,3));
assert(!compatible(2,2,3, 1,2,3));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment