Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

semver_satisfies 'caret' corner cases #20

Open
weber-martin opened this issue Jan 24, 2019 · 0 comments
Open

semver_satisfies 'caret' corner cases #20

weber-martin opened this issue Jan 24, 2019 · 0 comments

Comments

@weber-martin
Copy link

weber-martin commented Jan 24, 2019

From https://docs.npmjs.com/misc/semver#caret-ranges-123-025-004

^1.2.3 := >=1.2.3 <2.0.0

Note >= 1.2.3, i.e., !(< 1.2.3). 1.2.3-alpha < 1.2.3 per semver, but semver_satisfies(1.2.3, 1.2.3-alpha, ^) == 1.

Note that < 2.0.0 and 2.0.0-alpha < 2.0.0, but semver_satisfies(1.2.3, 2.0.0-alpha, ^) == 0

#include <assert.h>
#include <stdio.h>
#include "semver.h"

int satisfies(const char *sv1, const char *sv2, const char *op) {
	semver_t v1, v2;
	int rc;

	rc = semver_parse(sv1, &v1);
	assert(!rc);
	rc = semver_parse(sv2, &v2);
	assert(!rc);
	rc = semver_satisfies(v1, v2, op);
	semver_free(&v1);
	semver_free(&v2);
	return rc;
}

int main(int argc, char *argv[]) {
	struct bundle {
		const char *sv1;
		const char *sv2;
	} cases [] = {
		{"1.2.3", "1.2.3"},
		{"1.2.3", "1.2.3-alpha"}, /* 1.2.3 > 1.2.3-alpha ! */
		{"1.2.3", "2.0.0"},
		{"1.2.3", "2.0.0-alpha"}, /* 2.0.0-alpha < 2.0.0 ! */
	};
	size_t num_cases = sizeof(cases) / sizeof(struct bundle), i;

	for (i=0; i < num_cases; ++i)
		printf("%s ^ %s = %d\n", cases[i].sv1, cases[i].sv2, 
			satisfies(cases[i].sv1, cases[i].sv2, "^"));
	return 0;
}

outputs

1.2.3 ^ 1.2.3 = 1
1.2.3 ^ 1.2.3-alpha = 1 # should be 0
1.2.3 ^ 2.0.0 = 0
1.2.3 ^ 2.0.0-alpha = 0 # should be 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant