Repository: Konstantin8105/c4go
Pull request #315 (2019-03-13 11:14:09)

CSTD: string.h: add test for functions: strtok, strncmp

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=h1) Report
> Merging [#315](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/61e487a475dadcdf095762bdb5c6bc43e2fe38ec?src=pr&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `65.38%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/315/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master    #315      +/-   ##
=========================================
+ Coverage   85.96%     86%   +0.03%     
=========================================
  Files         188     188              
  Lines       10932   10936       +4     
=========================================
+ Hits         9398    9405       +7     
+ Misses       1092    1086       -6     
- Partials      442     445       +3
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/bind.go](https://codecov.io/gh/Konstantin8105/c4go/pull/315/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9iaW5kLmdv) | `69.41% <65.38%> (+2.54%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=footer). Last update [61e487a...9d6c911](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=h1) Report
> Merging [#315](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/61e487a475dadcdf095762bdb5c6bc43e2fe38ec?src=pr&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `65.38%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/315/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master    #315      +/-   ##
=========================================
+ Coverage   85.96%     86%   +0.03%     
=========================================
  Files         188     188              
  Lines       10932   10936       +4     
=========================================
+ Hits         9398    9405       +7     
+ Misses       1092    1086       -6     
- Partials      442     445       +3
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/bind.go](https://codecov.io/gh/Konstantin8105/c4go/pull/315/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9iaW5kLmdv) | `69.41% <65.38%> (+2.54%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=footer). Last update [61e487a...9d6c911](https://codecov.io/gh/Konstantin8105/c4go/pull/315?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index d52e538..261fd63 100644
--- a/README.md
+++ b/README.md
@@ -174,7 +174,7 @@ func c4goUnsafeConvert_int32(c4go_name *int32) []int32 {
 
 // frexp - add c-binding for implemention function
 func frexp(arg0 float64, arg1 []int32) float64 {
-	return float64(C.frexp(C.double(arg0), (*_Ctype_int)(unsafe.Pointer(&arg1[0]))))
+ 	return float64(C.frexp(C.double(arg0), (*C.int)(unsafe.Pointer(&arg1[0]))))
 }
 ```
 
@@ -320,7 +320,7 @@ func c4goUnsafeConvert_int32(c4go_name *int32) []int32 {
             stddef.h	       2/6	        33.3%
              stdio.h	     35/46	        76.1%
             stdlib.h	     33/47	        70.2%
-            string.h	     16/24	        66.7%
+            string.h	     21/24	        87.5%
               time.h	     14/15	        93.3%
              wchar.h	      3/68	        4.41%
             wctype.h	      0/22	           0%
diff --git a/scripts/neatpost.sh b/scripts/neatpost.sh
index 9b08fc7..6aebb77 100755
--- a/scripts/neatpost.sh
+++ b/scripts/neatpost.sh
@@ -23,6 +23,7 @@ mkdir -p ./testdata/
 		sed -i.bak '163,173d;158,161d;152,156d;145,150d;139,143d;133,137d;128,131d;123,127d;109,121d;90,107d;80,88d;58,78d;47,56d;42,45d;33,40d;8d' $TEMP_FOLDER/ps.c
 		sed -i.bak '219,232d;212,217d;204,210d;196,202d;179,194d;175,177d;131,173d' $TEMP_FOLDER/ps.c
 		sed -i.bak '238,262d;123,129d;116,121d;109,114d;102,107d;95,100d;82,93d;73,80d;65,71d;57,63d' $TEMP_FOLDER/ps.c
+		sed -i.bak '47d;46d;11d;10d;9d' $TEMP_FOLDER/ps.c
 	fi
 
 # remove go files from last transpilation
diff --git a/scripts/scripts.txt b/scripts/scripts.txt
index c824478..0dbdc1e 100644
--- a/scripts/scripts.txt
+++ b/scripts/scripts.txt
@@ -1,27 +1,27 @@
 
 Calculate warnings : ./testdata/9wm
-		After transpiling : 2 warnings.
-		Go build : 12 warnings
+		After transpiling : 4 warnings.
+		Go build : 4 warnings
 		Unsafe   : 407
 
 Calculate warnings : ./testdata/ed
-		After transpiling : 2 warnings.
-		Go build : 26 warnings
+		After transpiling : 3 warnings.
+		Go build : 2 warnings
 		Unsafe   : 169
 
 Calculate warnings in file: ./testdata/neateqn/neateqn.go
 		After transpiling : 0 warnings.
-		Go build : 3 warnings
+		Go build : 0 warnings
 		Unsafe   : 22
 
 Calculate warnings in file: ./testdata/neatmkfn/neatmkfn.go
-		After transpiling : 555 warnings.
-		Go build : 3 warnings
+		After transpiling : 557 warnings.
+		Go build : 72 warnings
 		Unsafe   : 44
 
 Calculate warnings in file: ./testdata/neatpost/neatpost.go
 		After transpiling : 0 warnings.
-		Go build : 9 warnings
+		Go build : 23 warnings
 		Unsafe   : 40
 
 Calculate warnings in file: ./testdata/neatrefer/neatrefer.go
@@ -31,12 +31,12 @@ Calculate warnings in file: ./testdata/neatrefer/neatrefer.go
 
 Calculate warnings in file: ./testdata/neatroff/neatroff.go
 		After transpiling : 1 warnings.
-		Go build : 6 warnings
+		Go build : 40 warnings
 		Unsafe   : 254
 
 Calculate warnings in file: ./testdata/neatvi/neatvi.go
-		After transpiling : 28 warnings.
-		Go build : 3 warnings
+		After transpiling : 34 warnings.
+		Go build : 12 warnings
 		Unsafe   : 130
 
 Calculate warnings in file: testdata/vorbis/vorbis.go
diff --git a/scripts/vorbis.sh b/scripts/vorbis.sh
index c1f941c..5aa1412 100755
--- a/scripts/vorbis.sh
+++ b/scripts/vorbis.sh
@@ -36,7 +36,7 @@ mkdir -p ./testdata/
 # show warnings comments in Go source
 	echo "Calculate warnings in file: $GO_FILE"
 	WARNINGS=`cat $GO_FILE | grep "^// Warning" | sort | uniq | wc -l`
-	echo "After transpiling : $WARNINGS warnings."
+	echo "		After transpiling : $WARNINGS warnings."
 
 # show other warnings
 	# show amount error from `go build`:
diff --git a/transpiler/bind.go b/transpiler/bind.go
index cf2d2dc..b8ed24d 100644
--- a/transpiler/bind.go
+++ b/transpiler/bind.go
@@ -59,9 +59,6 @@ func generateBinding(p *program.Program) (bindHeader, bindCode string) {
 		//		return float64(C.frexp(C.double(arg1), unsafe.Pointer(arg2)))
 		// }
 
-		mess := fmt.Sprintf("// %s - add c-binding for implemention function", ds[i].Name)
-		bindCode += mess
-
 		code, err := getBindFunction(p, ds[i])
 		if err != nil {
 			bindCode += p.GenerateWarningMessage(err, nil) + "\n"
@@ -126,28 +123,18 @@ func getBindFunction(p *program.Program, d program.DefinitionFunction) (code str
 		arg = append(arg, cgoExpr)
 	}
 
-	if returnResolvedType != "" {
-		f.Body = &goast.BlockStmt{
-			List: []goast.Stmt{
-				&goast.ReturnStmt{
-					Results: []goast.Expr{
-						util.NewCallExpr(returnResolvedType,
-							util.NewCallExpr(fmt.Sprintf("C.%s", d.Name), arg...),
-						),
-					},
-				},
-			},
-		}
-	} else {
-		f.Body = &goast.BlockStmt{
-			List: []goast.Stmt{
-				&goast.ReturnStmt{
-					Results: []goast.Expr{
-						util.NewCallExpr(fmt.Sprintf("C.%s", d.Name), arg...),
-					},
-				},
+	f.Body = &goast.BlockStmt{}
+
+	stmts := bindFromCtoGo(p, d.ReturnType, returnResolvedType, util.NewCallExpr(fmt.Sprintf("C.%s", d.Name), arg...))
+	f.Body.List = append(f.Body.List, stmts...)
+
+	// add comment for function
+	f.Doc = &goast.CommentGroup{
+		List: []*goast.Comment{
+			&goast.Comment{
+				Text: fmt.Sprintf("// %s - add c-binding for implemention function", d.Name),
 			},
-		}
+		},
 	}
 
 	var buf bytes.Buffer
@@ -261,7 +248,7 @@ func ResolveCgoType(p *program.Program, goType string, expr goast.Expr) (a goast
 			// TODO: check next
 			t = goType[2:]
 		}
-		t = "( * _Ctype_" + t + " ) "
+		t = "( * C." + t + " ) "
 		t = strings.Replace(t, " ", "", -1)
 
 		p.AddImport("unsafe")
@@ -285,7 +272,7 @@ func ResolveCgoType(p *program.Program, goType string, expr goast.Expr) (a goast
 			// TODO: check next
 			t = goType[1:]
 		}
-		t = "( * _Ctype_" + t + " ) "
+		t = "( * C." + t + " ) "
 		t = strings.Replace(t, " ", "", -1)
 
 		p.AddImport("unsafe")
@@ -316,3 +303,45 @@ func ResolveCgoType(p *program.Program, goType string, expr goast.Expr) (a goast
 		Args: []goast.Expr{expr},
 	}, nil
 }
+
+// example:
+//
+// returnValue := ...
+// return cast_from_C_to_Go_type(returnValue)
+//
+func bindFromCtoGo(p *program.Program, cType string, goType string, expr goast.Expr) (stmts []goast.Stmt) {
+
+	if expr == nil {
+		expr = goast.NewIdent("C4GO_UNDEFINE_EXPR")
+	}
+	if goType == "" {
+		goType = "C4GO_UNDEFINE_GO_TYPE"
+	}
+
+	if cType == "" {
+		stmts = append(stmts, &goast.ReturnStmt{Results: []goast.Expr{expr}})
+		return
+	}
+
+	// from documentation : https://golang.org/cmd/cgo/
+	//
+	// C string to Go string
+	// func C.GoString(*C.char) string
+	//
+
+	switch cType {
+	case "char *":
+		stmts = append(stmts, &goast.ReturnStmt{Results: []goast.Expr{
+			util.NewCallExpr("[]byte",
+				util.NewCallExpr("C.GoString", expr),
+			),
+		}})
+
+	default:
+		stmts = append(stmts, &goast.ReturnStmt{Results: []goast.Expr{
+			util.NewCallExpr(goType, expr),
+		}})
+	}
+
+	return
+}


Test patch:
diff --git a/scripts/test.sh b/scripts/test.sh
index f4df0ec..e83c7a8 100755
--- a/scripts/test.sh
+++ b/scripts/test.sh
@@ -15,7 +15,9 @@ export PKGS_DELIM=$(echo "$PKGS" | tr ' ' ',')
 echo "PKGS       : $PKGS"
 echo "PKGS_DELIM : $PKGS_DELIM"
 
-go test -v -cover -timeout=30m          \
+go test                                 \
+				  -cover                \
+				  -timeout=30m          \
 				  -tags integration     \
 	              -coverpkg=$PKGS_DELIM \
 				  -coverprofile=./testdata/pkg.coverprofile $PKGS
diff --git a/tests/string.c b/tests/string.c
index fe238a5..c54d661 100644
--- a/tests/string.c
+++ b/tests/string.c
@@ -1,6 +1,69 @@
 #include "tests.h"
 #include <string.h>
 
+#define START_TEST(t) \
+    diag(#t);         \
+    test_##t();
+
+void test_strtok()
+{
+	char str[] ="- This, a sample string.";
+	char * pch;
+	printf ("Splitting string \"%s\" into tokens:\n",str);
+	pch = strtok (str," ,.-");
+	printf("strtok: `%s`\n", pch);
+	// while (pch != NULL)
+	// {
+	  // printf ("%s\n",pch);
+	  // pch = strtok (NULL, " ,.-");
+	// }
+}
+
+void test_strncmp()
+{
+	char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
+	int n;
+	puts ("Looking for R2 astromech droids...");
+	for (n=0 ; n<3 ; n++) {
+		if (strncmp (str[n],"R2xx",2) == 0) {
+			printf ("found %s\n",str[n]);
+		}
+	}
+}
+
+void test_strcspn()
+{
+	char str[] = "fcba73";
+	char keys[] = "1234567890";
+	int i;
+	i = strcspn (str,keys);
+	printf ("The first number in str is at position %d.\n",i+1);
+}
+
+void test_strpbrk()
+{
+	char str[] = "This is a sample string";
+	char key[] = "aeiou";
+	char * pch;
+	printf ("Vowels in '%s': ",str);
+	pch = strpbrk (str, key);
+	printf("strpbrk: `%s`\n", pch);
+	// while (pch != NULL) {
+		// printf ("%c " , *pch);
+		// pch = strpbrk (pch+1,key);
+	// }
+	printf ("\n");
+}
+
+void test_strspn()
+{
+	int i;
+	char strtext[] = "129th";
+	char cset[] = "1234567890";
+	i = strspn (strtext,cset);
+	printf ("The initial number has %d digits.\n",i);
+}
+
 int main()
 {
     plan(43);
@@ -191,6 +254,11 @@ int main()
         is_streq(s1, s2);
         is_true(s1 != s2);
     }
+	START_TEST(strtok);
+	START_TEST(strncmp);
+	START_TEST(strcspn);
+	START_TEST(strpbrk);
+	START_TEST(strspn);
     {
         diag("strerror");
         strerror(0);