[cmor] 164/190: Issue: #4 Marks changes with some minor refactoring to seperate out test cases
Alastair McKinstry
mckinstry at moszumanska.debian.org
Tue Jul 21 12:54:50 UTC 2015
This is an automated email from the git hooks/post-receive script.
mckinstry pushed a commit to branch debian/master
in repository cmor.
commit 0f7f570775e8f6ee0d09bd5506c5c47f30e91f6d
Author: Jamie Kettleborough <jamie.kettleborough at metoffice.gov.uk>
Date: Mon Nov 11 16:33:22 2013 +0000
Issue: #4 Marks changes with some minor refactoring to seperate out test cases
---
Makefile.in | 1 +
Src/cmor.c | 50 +++++++++++++++---
Test/test_unit_cat_unique.c | 122 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 165 insertions(+), 8 deletions(-)
diff --git a/Makefile.in b/Makefile.in
index f45a18d..2bb0a88 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -94,6 +94,7 @@ test_C: cmor
@rm -f ./ipcc_test_code ; @CC@ @CFLAGS@ @USERCFLAGS@ @CPPFLAGS@ Test/ipcc_test_code.c -L at prefix@/lib -I at prefix@/include -L. -lcmor @NCCFLAGS@ @NCLDFLAGS@ @UDUNITS2LDFLAGS@ @UDUNITS2FLAGS@ @UUIDLDFLAGS@ @UUIDFLAGS@ -o ipcc_test_code @VERB@; ./ipcc_test_code @VERB@
@rm -f test_grid ; @CC@ @CFLAGS@ @USERCFLAGS@ @CPPFLAGS@ Test/test_grid.c -L at prefix@/lib -I at prefix@/include -L. -lcmor @NCCFLAGS@ @NCLDFLAGS@ @UDUNITS2LDFLAGS@ @UDUNITS2FLAGS@ @UUIDLDFLAGS@ @UUIDFLAGS@ -o test_grid @VERB@; ./test_grid @VERB@;
@rm -f test_lots_of_variables ; @CC@ @CFLAGS@ @USERCFLAGS@ @CPPFLAGS@ Test/test_lots_of_variables.c -L at prefix@/lib -I at prefix@/include -L. -lcmor @NCCFLAGS@ @NCLDFLAGS@ @UDUNITS2LDFLAGS@ @UDUNITS2FLAGS@ @UUIDLDFLAGS@ @UUIDFLAGS@ -o test_lots_of_variables @VERB@; ./test_lots_of_variables @VERB@;
+ @rm -f test_cat_unique ; @CC@ @CFLAGS@ @USERCFLAGS@ @CPPFLAGS@ Test/test_unit_cat_unique.c -L at prefix@/lib -I at prefix@/include -L. -lcmor @NCCFLAGS@ @NCLDFLAGS@ @UDUNITS2LDFLAGS@ @UDUNITS2FLAGS@ @UUIDLDFLAGS@ @UUIDFLAGS@ -o test_unit_cat_unique @VERB@; ./test_unit_cat_unique @VERB@;
python:
@echo "Building Python interface"
@${PYTHONEXEC} setup.py install @CDATPREFIX@
diff --git a/Src/cmor.c b/Src/cmor.c
index c1ca361..9bdc370 100644
--- a/Src/cmor.c
+++ b/Src/cmor.c
@@ -55,6 +55,40 @@ int CMOR_CREATE_SUBDIRECTORIES = 1;
char cmor_input_path[CMOR_MAX_STRING];
char cmor_traceback_info[CMOR_MAX_STRING];
+void cmor_cat_unique_string (char* dest, char* src) {
+ char* pstr=dest;
+ int destlen=strlen(dest);
+ int spare_space=CMOR_MAX_STRING-destlen;
+ /* if this string is in msg */
+ while (pstr=strstr(pstr, src)) {
+ /* if this word is at the beginning of msg or preceeded by a space */
+ if (pstr==dest || pstr[-1]==' ') {
+ /* if it isn't a substring match */
+ if ((pstr[strlen(src)] != ' ') &&
+ (pstr[strlen(src)] != 0)) {
+ /* then add this string to the end of msg */
+ strncat(pstr, " " ,spare_space);
+ spare_space--;
+ strncat(pstr, src, spare_space);
+ }
+ break; /* Either existed here or didn't*/
+ }
+ /* If it gets here, then it was a partial match, like "rum" and "rumble". */
+ pstr++;/* In which case, skip to the next char */
+ spare_space=CMOR_MAX_STRING-(pstr-dest); /* shorten the space available for the rest of the destination string */
+ /* and go round again */
+ }
+ if (pstr==0) {
+ /* this string is not in msg, therefore append it */
+ if (dest[0]!=0) {
+ /* This is being appended to a preexisting string, so separate with a space */
+ strncat(dest, " ", spare_space);
+ spare_space--;
+ }
+ strncat(dest, src, spare_space);
+ }
+}
+
void cmor_check_forcing_validity(int table_id,char *value) {
int i,j,n,found=0;
char msg[CMOR_MAX_STRING];
@@ -1766,6 +1800,7 @@ int cmor_write(int var_id,void *data, char type, char *suffix, int ntimes_passed
uuid_fmt_t fmt;
void *myuuid_str=NULL;
size_t uuidlen;
+
extern int cmor_convert_char_to_hyphen(char c);
@@ -2854,15 +2889,14 @@ int cmor_write(int var_id,void *data, char type, char *suffix, int ntimes_passed
if (cmor_axes[cmor_vars[var_id].axes_ids[k]].isgridaxis==1) {
nc_dims_associated[l]=nc_dim_af[k];
/* printf("ok we have a grid axis %s associated with dim %i (k is: %i)\n",cmor_axes[cmor_vars[var_id].axes_ids[k]].id,l,k); */
+
if (m2[i]==0 && (i==0 || i==1)) {
- if (cmor_has_variable_attribute(var_id,"coordinates")==0) {
- cmor_get_variable_attribute(var_id,"coordinates",&msg);
- strncat(msg," ",CMOR_MAX_STRING-strlen(msg));
- strncat(msg,cmor_vars[cmor_grids[cmor_vars[var_id].grid_id].associated_variables[i]].id,CMOR_MAX_STRING-strlen(msg));
- }
- else {
- strncpy(msg,cmor_vars[cmor_grids[cmor_vars[var_id].grid_id].associated_variables[i]].id,CMOR_MAX_STRING-strlen(msg));
- }
+ if (cmor_has_variable_attribute(var_id,"coordinates")==0) {
+ cmor_get_variable_attribute(var_id,"coordinates",&msg);
+ cmor_cat_unique_string(msg, cmor_vars[cmor_grids[cmor_vars[var_id].grid_id].associated_variables[i]].id);
+ } else {
+ strncpy(msg,cmor_vars[cmor_grids[cmor_vars[var_id].grid_id].associated_variables[i]].id,CMOR_MAX_STRING-strlen(msg));
+ }
cmor_set_variable_attribute_internal(var_id,"coordinates",'c',msg);
m2[i]=1;
}
diff --git a/Test/test_unit_cat_unique.c b/Test/test_unit_cat_unique.c
new file mode 100644
index 0000000..973a1e1
--- /dev/null
+++ b/Test/test_unit_cat_unique.c
@@ -0,0 +1,122 @@
+#include <string.h>
+#include "cmor.h"
+
+extern void cmor_cat_unique_string (char* dest, char* src);
+
+int test_cat_unique_string(void) {
+ char dest[128];
+ char src[128];
+ char expected[128];
+ strcpy(dest, "");
+ strcpy(src, "rumble");
+ strcpy(expected, "rumble");
+
+ /* 1. simple test: add string to blank */
+ printf("running tests\n");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 1;
+ }
+
+ /* 2. simple test: add string to itself. Should be identical to above */
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 2;
+ }
+
+ /* 3. simple test: add string to non-blank, unique*/
+ strcpy(src, "jungle");
+ strcpy(expected, "rumble jungle");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 3;
+ }
+
+ /* 4. simple test: add string that exists within another word */
+ strcpy(dest, "rumble jungle");
+ strcpy(src, "umb");
+ strcpy(expected, "rumble jungle umb");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 4;
+ }
+
+ /* 5. simple test: add string to blank */
+ strcpy(dest, "rumble");
+ strcpy(src, "rum");
+ strcpy(expected, "rumble rum");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 5;
+ }
+
+ /* 6. simple test: add string to blank */
+ strcpy(dest, "rumble");
+ strcpy(src, "ble");
+ strcpy(expected, "rumble ble");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 6;
+ }
+
+ /* 7. simple test: add string to blank */
+ strcpy(dest, "rumble jungle happy");
+ strcpy(src, "ppy");
+ strcpy(expected, "rumble jungle happy ppy");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 7;
+ }
+
+ /* 8. simple test: add string to blank */
+ strcpy(dest, "rumble jungle happy");
+ strcpy(src, "gle");
+ strcpy(expected, "rumble jungle happy gle");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 8;
+ }
+
+ /* 9. simple test: add string to blank */
+ strcpy(dest, "rumble jungle happy");
+ strcpy(src, "jung");
+ strcpy(expected, "rumble jungle happy jung");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 9;
+ }
+ /* 10. simple test: add string to blank */
+ strcpy(dest, "rumble jumble ble");
+ strcpy(src, "ble");
+ strcpy(expected, "rumble jumble ble");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 9;
+ }
+
+ /* 11. simple test: add latest */
+ strcpy(dest, "rumble jumble");
+ strcpy(src, "jumble");
+ strcpy(expected, "rumble jumble");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 10;
+ }
+
+ /* 12. simple test: add latest */
+ strcpy(dest, "rumble jumble");
+ strcpy(src, "rumble");
+ strcpy(expected, "rumble jumble");
+ cmor_cat_unique_string(dest,src);
+ if (strcmp(dest,expected)) {
+ return 11;
+ }
+
+ return 0;
+
+}
+
+int main(int argc, char **argv) {
+ test_cat_unique_string();
+
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/cmor.git
More information about the debian-science-commits
mailing list