Repository: Konstantin8105/c4go
Pull request #419 (2019-04-22 12:24:30)

CSTD: redesign function realloc

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/419?src=pr&el=h1) Report
> Merging [#419](https://codecov.io/gh/Konstantin8105/c4go/pull/419?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/1dc5b3dcfb0d8a46f99552e8532c6435bbffbeb2?src=pr&el=desc) will **decrease** coverage by `0.05%`.
> The diff coverage is `81.81%`.

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

```diff
@@            Coverage Diff             @@
##           master     #419      +/-   ##
==========================================
- Coverage   84.78%   84.73%   -0.06%     
==========================================
  Files         191      191              
  Lines       11595    11582      -13     
==========================================
- Hits         9831     9814      -17     
- Misses       1282     1284       +2     
- Partials      482      484       +2
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/419?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [program/definition\_function.go](https://codecov.io/gh/Konstantin8105/c4go/pull/419/diff?src=pr&el=tree#diff-cHJvZ3JhbS9kZWZpbml0aW9uX2Z1bmN0aW9uLmdv) | `88.77% <ø> (ø)` | :arrow_up: |
| [transpiler/call.go](https://codecov.io/gh/Konstantin8105/c4go/pull/419/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9jYWxsLmdv) | `76.79% <ø> (-1.18%)` | :arrow_down: |
| [noarch/stdlib.go](https://codecov.io/gh/Konstantin8105/c4go/pull/419/diff?src=pr&el=tree#diff-bm9hcmNoL3N0ZGxpYi5nbw==) | `91.44% <100%> (+0.33%)` | :arrow_up: |
| [noarch/string.go](https://codecov.io/gh/Konstantin8105/c4go/pull/419/diff?src=pr&el=tree#diff-bm9hcmNoL3N0cmluZy5nbw==) | `86.91% <50%> (-1.55%)` | :arrow_down: |
| [debug.go](https://codecov.io/gh/Konstantin8105/c4go/pull/419/diff?src=pr&el=tree#diff-ZGVidWcuZ28=) | `79.88% <0%> (-0.6%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/419?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/419?src=pr&el=footer). Last update [1dc5b3d...e3dbe29](https://codecov.io/gh/Konstantin8105/c4go/pull/419?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/noarch/stdio.go b/noarch/stdio.go
index a45ad5e..8eb81dc 100644
--- a/noarch/stdio.go
+++ b/noarch/stdio.go
@@ -859,7 +859,7 @@ func Perror(msg []byte) {
 	fmt.Fprintf(os.Stderr, "%s: No such file or directory\n", m)
 }
 
-func Getline(line [][]byte, len []uint, f *File) SsizeT {
+func Getline(line [][]byte, len []uint32, f *File) SsizeT {
 	counter := 0
 	for {
 		buf := make([]byte, 1)
diff --git a/noarch/string.go b/noarch/string.go
index 5e38d2c..b6f81f1 100644
--- a/noarch/string.go
+++ b/noarch/string.go
@@ -2,7 +2,6 @@ package noarch
 
 import (
 	"bytes"
-	"reflect"
 	"unsafe"
 )
 
@@ -181,23 +180,6 @@ func Memcmp(lhs []byte, rhs []byte, count uint32) int32 {
 	return 0
 }
 
-func Memcpy(dst, src interface{}, size uint) interface{} {
-	switch reflect.TypeOf(src).Kind() {
-	case reflect.Slice:
-		s := reflect.ValueOf(src)
-		d := reflect.ValueOf(dst)
-		size /= uint(int(s.Index(0).Type().Size()))
-		var val reflect.Value
-		for i := 0; i < int(size); i++ {
-			if i < s.Len() {
-				val = s.Index(i)
-			}
-			d.Index(i).Set(val)
-		}
-	}
-	return dst
-}
-
 func Strrchr(source []byte, c32 int32) []byte {
 	c := int(c32)
 	ch := byte(c)
diff --git a/noarch/unistd.go b/noarch/unistd.go
index 7f22907..6499779 100644
--- a/noarch/unistd.go
+++ b/noarch/unistd.go
@@ -39,7 +39,7 @@ func Pipe(p []int32) int32 {
 	return 0
 }
 
-func Read(fd int32, p []byte, num uint) SsizeT {
+func Read(fd int32, p []byte, num uint32) SsizeT {
 	if num == 0 {
 		return 0
 	}
@@ -62,7 +62,7 @@ func Read(fd int32, p []byte, num uint) SsizeT {
 	return SsizeT(n)
 }
 
-func Write(fd int32, p []byte, num uint) SsizeT {
+func Write(fd int32, p []byte, num uint32) SsizeT {
 	p = p[:num]
 	n, err := syscall.Write(int(fd), p)
 	if err != nil {
diff --git a/program/definition_function.go b/program/definition_function.go
index c63f93d..fb6fd11 100644
--- a/program/definition_function.go
+++ b/program/definition_function.go
@@ -277,7 +277,6 @@ var builtInFunctionDefinitions = map[string][]string{
 		"char * memset(char *, char, unsigned int) -> noarch.Memset",
 		"char * memmove(char *, char *, unsigned int) -> noarch.Memmove",
 		"int memcmp(const char *, const char *, unsigned int) -> noarch.Memcmp",
-		"void * memcpy(void *, const void *, size_t) -> noarch.Memcpy",
 		"const char * strrchr( const char *, int) -> noarch.Strrchr",
 		"char * strdup(const char *) -> noarch.Strdup",
 		"char * strerror(int ) -> noarch.Strerror",
@@ -540,6 +539,12 @@ func (p *Program) GetOutsideCalledFunctions() (ds []DefinitionFunction) {
 		if !v.IsCalled {
 			continue
 		}
+		if v.Name == "memcpy" && p.IsHaveMemcpy {
+			continue
+		}
+		if v.Name == "realloc" && p.IsHaveRealloc {
+			continue
+		}
 		ds = append(ds, v)
 	}
 	return
diff --git a/program/definition_type.go b/program/definition_type.go
index 4a85250..2348cb5 100644
--- a/program/definition_type.go
+++ b/program/definition_type.go
@@ -40,7 +40,7 @@ var DefinitionType = map[string]string{
 	"unsigned short int":     "uint16",
 	"void":                   "",
 	"_Bool":                  "int32",
-	"size_t":                 "uint",
+	"size_t":                 "uint32",
 	"ptrdiff_t":              "github.com/Konstantin8105/c4go/noarch.PtrdiffT",
 	"github.com/Konstantin8105/c4go/noarch.PtrdiffT": "uint64",
 	"wchar_t": "github.com/Konstantin8105/c4go/noarch.WcharT",
diff --git a/program/program.go b/program/program.go
index a0770ff..a8a197d 100644
--- a/program/program.go
+++ b/program/program.go
@@ -112,6 +112,9 @@ type Program struct {
 	IsHaveVaList bool
 
 	DoNotAddComments bool
+
+	IsHaveMemcpy  bool
+	IsHaveRealloc bool
 }
 
 type commentPos struct {
@@ -148,6 +151,8 @@ func NewProgram() (p *Program) {
 		builtInFunctionDefinitionsHaveBeenLoaded: false,
 		UnsafeConvertValueToPointer:              map[string]bool{},
 		UnsafeConvertPointerArith:                map[string]bool{},
+		IsHaveMemcpy:                             false,
+		IsHaveRealloc:                            false,
 	}
 }
 
diff --git a/scripts/scripts.txt b/scripts/scripts.txt
index 9a28296..097bb96 100644
--- a/scripts/scripts.txt
+++ b/scripts/scripts.txt
@@ -21,7 +21,7 @@ Calculate warnings in file: ./testdata/minmad/minmad.go
 
 Calculate warnings in file: ./testdata/neateqn/neateqn.go
 		After transpiling : 0 warnings.
-		Go build : 6 warnings
+		Go build : 0 warnings
 		Unsafe   : 25
 
 Calculate warnings in file: ./testdata/neatmkfn/neatmkfn.go
@@ -36,7 +36,7 @@ Calculate warnings in file: ./testdata/neatmail/neatmail.go
 
 Calculate warnings in file: ./testdata/neatpost/neatpost.go
 		After transpiling : 0 warnings.
-		Go build : 31 warnings
+		Go build : 32 warnings
 		Unsafe   : 47
 
 Calculate warnings in file: ./testdata/neatrefer/neatrefer.go
@@ -46,7 +46,7 @@ Calculate warnings in file: ./testdata/neatrefer/neatrefer.go
 
 Calculate warnings in file: ./testdata/neatroff/neatroff.go
 		After transpiling : 3 warnings.
-		Go build : 16 warnings
+		Go build : 17 warnings
 		Unsafe   : 252
 
 Calculate warnings in file: ./testdata/neatvi/neatvi.go
@@ -62,7 +62,7 @@ Calculate warnings in file: ./testdata/triangle/triangle.go
 Calculate warnings in file: ./testdata/SQLITE/sqlite.go
 		After transpiling : 1 warnings.
 		Go build : 1 warnings
-		Unsafe   : 2407
+		Unsafe   : 2405
 
 Calculate warnings in file: testdata/vorbis/vorbis.go
 		After transpiling : 0 warnings.
diff --git a/transpiler/call.go b/transpiler/call.go
index 83ddb08..8075cf4 100644
--- a/transpiler/call.go
+++ b/transpiler/call.go
@@ -251,60 +251,15 @@ func transpileCallExpr(n *ast.CallExpr, p *program.Program) (
 		}
 	}
 
-	//
-	// Change from "realloc" to "calloc"
-	//
-	// CallExpr <> 'void *'
-	// |-ImplicitCastExpr <> 'void *(*)(void *, unsigned long)' <FunctionToPointerDecay>
-	// | `-DeclRefExpr <> 'void *(void *, unsigned long)' Function 0x2c7e3e0 'realloc' 'void *(void *, unsigned long)'
-	// |-ImplicitCastExpr <> 'void *' <BitCast>
-	// | `-CStyleCastExpr <> 'char *' <BitCast>
-	// |   `-ParenExpr <> 'void *'
-	// |     `-ParenExpr <> 'void *'
-	// |       `-CStyleCastExpr <> 'void *' <NullToPointer>
-	// |         `-IntegerLiteral <> 'int' 0
-	// `-ImplicitCastExpr <> 'unsigned long' <IntegralCast>
-	//   `-BinaryOperator <> 'int' '*'
-	//     |-ImplicitCastExpr <> 'int' <LValueToRValue>
-	//     | `-DeclRefExpr <> 'int' lvalue Var 0x2ca14e8 'size' 'int'
-	//     `-ImplicitCastExpr <> 'int' <LValueToRValue>
-	//       `-DeclRefExpr <> 'int' lvalue Var 0x2ca14e8 'size' 'int'
-	//
-	// CallExpr <> 'void *'
-	// |-ImplicitCastExpr <> 'void *(*)(unsigned long, unsigned long)' <FunctionToPointerDecay>
-	// | `-DeclRefExpr <> 'void *(unsigned long, unsigned long)' Function 'calloc' 'void *(unsigned long, unsigned long)'
-	// |-ImplicitCastExpr <> 'unsigned long' <IntegralCast>
-	// | `- ...
-	// `-UnaryExprOrTypeTraitExpr <> 'unsigned long' sizeof 'char'
-	//
-	// function "realloc" from stdlib.h
-	if p.IncludeHeaderIsExists("stdlib.h") {
-		if functionName == "realloc" && len(n.Children()) == 3 {
-			n.ChildNodes[0] = &ast.ImplicitCastExpr{
-				Type: "void *(*)(unsigned long, unsigned long)",
-			}
-			n.ChildNodes[0].(*ast.ImplicitCastExpr).AddChild(&ast.DeclRefExpr{
-				Type: "void *(unsigned long, unsigned long)",
-				Name: "calloc",
-			})
-			sizeofType := "char"
-			if impl, ok := n.Children()[1].(*ast.ImplicitCastExpr); ok {
-				switch v := impl.Children()[0].(type) {
-				case *ast.ImplicitCastExpr:
-					sizeofType = v.Type
-				case *ast.CStyleCastExpr:
-					sizeofType = v.Type
-				}
-			}
-			sizeofType = types.GetBaseType(sizeofType)
-			n.AddChild(&ast.UnaryExprOrTypeTraitExpr{
-				Type1:    "unsigned long",
-				Function: "sizeof",
-				Type2:    sizeofType,
-			})
-			n.ChildNodes = []ast.Node{n.ChildNodes[0], n.ChildNodes[2], n.ChildNodes[3]}
-			return transpileCallExpr(n, p)
-		}
+	if p.IncludeHeaderIsExists("stdlib.h") && functionName == "realloc" {
+		p.IsHaveRealloc = true
+		p.IsHaveMemcpy = true
+		p.SetCalled(functionName)
+	}
+
+	if p.IncludeHeaderIsExists("string.h") && functionName == "memcpy" {
+		p.IsHaveMemcpy = true
+		p.SetCalled(functionName)
 	}
 
 	// function "calloc" from stdlib.h
diff --git a/transpiler/memory.go b/transpiler/memory.go
new file mode 100644
index 0000000..935f7b1
--- /dev/null
+++ b/transpiler/memory.go
@@ -0,0 +1,46 @@
+package transpiler
+
+// realloc from `stdlib.h`
+const realloc string = `
+
+// realloc is function from stdlib.h.
+// generated by c4go.
+func realloc(ptr interface{}, size uint32) interface{} {
+	if ptr == nil {
+		return make([]byte, size)
+	}
+	elemType := reflect.TypeOf(ptr).Elem()
+	ptrNew := reflect.MakeSlice(reflect.SliceOf(elemType), int(size), int(size)).Interface()
+	// copy elements
+	memcpy(ptrNew, ptr, size)
+	return ptrNew
+}
+`
+
+// memcpy from `string.h`
+const memcpy string = `
+
+// memcpy is function from string.h.
+// generated by c4go.
+func memcpy(dst, src interface{}, size uint32) interface{} {
+	switch reflect.TypeOf(src).Kind() {
+	case reflect.Slice:
+		s := reflect.ValueOf(src)
+		d := reflect.ValueOf(dst)
+		if s.Len() == 0 {
+			return dst
+		}
+		if s.Len() > 0 {
+			size /= uint32(int(s.Index(0).Type().Size()))
+		}
+		var val reflect.Value
+		for i := 0; i < int(size); i++ {
+			if i < s.Len() {
+				val = s.Index(i)
+			}
+			d.Index(i).Set(val)
+		}
+	}
+	return dst
+}
+`
diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go
index a294e93..d0bf3d7 100644
--- a/transpiler/transpiler.go
+++ b/transpiler/transpiler.go
@@ -126,6 +126,11 @@ func TranspileAST(fileName, packageName string, withOutsideStructs bool,
 	// checking implementation for all called functions
 	bindHeader, bindCode := generateBinding(p)
 
+	// for memcpy and realloc
+	if p.IsHaveMemcpy || p.IsHaveRealloc {
+		p.AddImport("reflect")
+	}
+
 	// Add the imports after everything else so we can ensure that they are all
 	// placed at the top.
 	for _, quotedImportPath := range p.Imports() {
@@ -168,6 +173,13 @@ func TranspileAST(fileName, packageName string, withOutsideStructs bool,
 	// generate pointer arithmetic functions
 	source += getPointerArithFunctions(p)
 
+	if p.IsHaveMemcpy || p.IsHaveRealloc {
+		source += memcpy
+	}
+	if p.IsHaveRealloc {
+		source += realloc
+	}
+
 	return
 }
 
diff --git a/transpiler/value_to_pointer.go b/transpiler/value_to_pointer.go
index 25f22ab..aab721f 100644
--- a/transpiler/value_to_pointer.go
+++ b/transpiler/value_to_pointer.go
@@ -703,7 +703,7 @@ func pointerArithmetic(p *program.Program,
 	src := `package main
 func main(){
 	a := func()[]{{ .Type }} {
-		var position int64 = int64({{ .Condition }})
+		var position int32 = int32({{ .Condition }})
 		slice := {{ .Name }}
 		if position < 0 {
 			// invert sign


Test patch:
diff --git a/tests/ap.c b/tests/ap.c
index 845b4d7..335f816 100644
--- a/tests/ap.c
+++ b/tests/ap.c
@@ -108,12 +108,12 @@ int main()
     is_eq(*pnt, 123);
     is_eq(num, 123);
 
-// TODO: test for negative index
-//   diag("pointer arithmetic 7");
-//   int* tmp_i2 = i2 + 1;
-//   int* i13 = tmp_i2 + negative();
-//   a(i13);
-//   b(i13, 1);
+    // TODO: test for negative index
+    //   diag("pointer arithmetic 7");
+    //   int* tmp_i2 = i2 + 1;
+    //   int* i13 = tmp_i2 + negative();
+    //   a(i13);
+    //   b(i13, 1);
 
     done_testing();
 }
diff --git a/tests/array.c b/tests/array.c
index f871705..85cbca3 100644
--- a/tests/array.c
+++ b/tests/array.c
@@ -567,7 +567,7 @@ void test_typedef_pointer()
 
 int** getArray()
 {
-    int twod[5][4] = { { 2, 4, 6 , 7}, { 8, 10, 12,13 }, { 14, 16, 18, 19 }, { 20, 22, 24, 25 }, {26,28,30,32} };
+    int twod[5][4] = { { 2, 4, 6, 7 }, { 8, 10, 12, 13 }, { 14, 16, 18, 19 }, { 20, 22, 24, 25 }, { 26, 28, 30, 32 } };
     int* twodPass[5];
     twodPass[0] = twod[0];
     twodPass[1] = twod[1];
@@ -586,7 +586,7 @@ void view_matrix(int** p, int size1, int size2)
     }
 }
 
-// TODO : it is not Ok for Debug case 
+// TODO : it is not Ok for Debug case
 void test_double_array()
 {
     // see https://forums.macrumors.com/threads/understanding-double-pointers-in-c.701091/
@@ -595,71 +595,71 @@ void test_double_array()
     // printf("*p + 1 is: %d\n", *(*p + 1));
     // p = getArray();
     // {
-        // diag("cases 1:");
-        // int* pp = *p;
-        // printf("    1: %d\n", *(pp++));
-        // printf("    2: %d\n", *(pp++));
-        // printf("    3: %d\n", *(pp++));
+    // diag("cases 1:");
+    // int* pp = *p;
+    // printf("    1: %d\n", *(pp++));
+    // printf("    2: %d\n", *(pp++));
+    // printf("    3: %d\n", *(pp++));
     // }
     // TODO : view_matrix(p,4,3);
 
     // p = getArray();
     // {
-        // diag("cases 1a:");
-        // int* pp = *p;
-        // printf("    1: %d\n", (*pp)++);
-        // printf("    2: %d\n", (*pp)++);
-        // printf("    3: %d\n", (*pp)++);
+    // diag("cases 1a:");
+    // int* pp = *p;
+    // printf("    1: %d\n", (*pp)++);
+    // printf("    2: %d\n", (*pp)++);
+    // printf("    3: %d\n", (*pp)++);
     // }
     // TODO : view_matrix(p,4,3);
 
     // p = getArray();
     // {
-        // diag("cases 2:");
-        // int** pp = p;
-        // printf("    1: %d\n", *((*(pp))++));
-        // printf("    2: %d\n", *((*(pp))++));
-        // printf("    3: %d\n", *((*(pp))++));
+    // diag("cases 2:");
+    // int** pp = p;
+    // printf("    1: %d\n", *((*(pp))++));
+    // printf("    2: %d\n", *((*(pp))++));
+    // printf("    3: %d\n", *((*(pp))++));
     // }
     // TODO : view_matrix(p,4,3);
 
     // p = getArray();
     // {
-        // diag("cases 3:");
-        // int** pp = p;
-        // printf("    1: %d\n", (*pp)[0]);
-        // printf("    2: %d\n", (*pp)[1]);
-        // printf("    3: %d\n", (*pp)[2]);
+    // diag("cases 3:");
+    // int** pp = p;
+    // printf("    1: %d\n", (*pp)[0]);
+    // printf("    2: %d\n", (*pp)[1]);
+    // printf("    3: %d\n", (*pp)[2]);
     // }
     // TODO : view_matrix(p,4,3);
 
     // p = getArray();
     // {
-        // diag("cases 4:");
-        // int** pp = p;
-        // printf("    1: %d\n", *(*((pp)++)));
-        // printf("    2: %d\n", *(*((pp)++)));
-        // printf("    3: %d\n", *(*((pp)++)));
+    // diag("cases 4:");
+    // int** pp = p;
+    // printf("    1: %d\n", *(*((pp)++)));
+    // printf("    2: %d\n", *(*((pp)++)));
+    // printf("    3: %d\n", *(*((pp)++)));
     // }
     // TODO : view_matrix(p,4,3);
 
     // p = getArray();
     // {
-        // diag("cases 5:");
-        // int** pp = p;
-        // printf("    1: %d\n", *((*pp)++));
-        // printf("    2: %d\n", *((*pp)++));
-        // printf("    3: %d\n", *((*pp)++));
+    // diag("cases 5:");
+    // int** pp = p;
+    // printf("    1: %d\n", *((*pp)++));
+    // printf("    2: %d\n", *((*pp)++));
+    // printf("    3: %d\n", *((*pp)++));
     // }
     // TODO : view_matrix(p,4,3);
 
     // p = getArray();
     // {
-        // diag("cases 6:");
-        // int** pp = p;
-        // printf("    1: %d\n", pp[0][0]);
-        // printf("    2: %d\n", pp[0][1]);
-        // printf("    3: %d\n", pp[0][2]);
+    // diag("cases 6:");
+    // int** pp = p;
+    // printf("    1: %d\n", pp[0][0]);
+    // printf("    2: %d\n", pp[0][1]);
+    // printf("    3: %d\n", pp[0][2]);
     // }
     // TODO : view_matrix(p,4,3);
 }
@@ -735,47 +735,47 @@ void test_post_pointer()
 }
 
 struct po_ni {
-	int * fl;
+    int* fl;
 };
 
 void test_array_nil()
 {
-	struct po_ni ss;
-	ss.fl = NULL;
-	struct po_ni *s = &ss;
-	if (s->fl) {
-		fail("array_nil");
-	} else {
-		pass("array_nil");
-	}
-	if (s->fl == NULL) {
-		pass("array_nil");
-	} else {
-		fail("array_nil");
-	}
-	if (s->fl != NULL) {
-		fail("array_nil");
-	} else {
-		pass("array_nil");
-	}
-
-	int y = 42;
-	ss.fl = &y;
-	if (s->fl) {
-		pass("array_nil");
-	} else {
-		fail("array_nil");
-	}
-	if (s->fl == NULL) {
-		fail("array_nil");
-	} else {
-		pass("array_nil");
-	}
-	if (s->fl != NULL) {
-		pass("array_nil");
-	} else {
-		fail("array_nil");
-	}
+    struct po_ni ss;
+    ss.fl = NULL;
+    struct po_ni* s = &ss;
+    if (s->fl) {
+        fail("array_nil");
+    } else {
+        pass("array_nil");
+    }
+    if (s->fl == NULL) {
+        pass("array_nil");
+    } else {
+        fail("array_nil");
+    }
+    if (s->fl != NULL) {
+        fail("array_nil");
+    } else {
+        pass("array_nil");
+    }
+
+    int y = 42;
+    ss.fl = &y;
+    if (s->fl) {
+        pass("array_nil");
+    } else {
+        fail("array_nil");
+    }
+    if (s->fl == NULL) {
+        fail("array_nil");
+    } else {
+        pass("array_nil");
+    }
+    if (s->fl != NULL) {
+        pass("array_nil");
+    } else {
+        fail("array_nil");
+    }
 }
 
 int main()
@@ -1271,7 +1271,7 @@ int main()
         (void)(t);
     }
 
-    test_pointer_arith_size_t();
+    START_TEST(pointer_arith_size_t);
     START_TEST(pointer_minus_pointer);
 
     diag("calloc with struct");
@@ -1320,7 +1320,7 @@ int main()
     START_TEST(negative_index);
     START_TEST(matrix_init);
     START_TEST(post_pointer);
-	START_TEST(array_nil);
+    START_TEST(array_nil);
 
     done_testing();
 }
diff --git a/tests/sizeof.c b/tests/sizeof.c
index ac51dcc..f63a8a3 100644
--- a/tests/sizeof.c
+++ b/tests/sizeof.c
@@ -3,11 +3,10 @@
 #include "tests.h"
 #include <stdio.h>
 
-#define is_not_less(arg1, arg2)\
-	is_true(arg1 >= arg2) \
-	// printf("arg1 = %d\n",arg1);
+#define is_not_less(arg1, arg2) \
+    is_true(arg1 >= arg2) // printf("arg1 = %d\n",arg1);
 
-#define check_sizes(type, size)         \
+#define check_sizes(type, size)               \
     is_not_less(sizeof(type), size);          \
     is_not_less(sizeof(unsigned type), size); \
     is_not_less(sizeof(signed type), size);   \
@@ -50,67 +49,66 @@ struct s {
     FILE* p;
 };
 
-
 typedef struct erow {
-    int idx;            /* Row index in the file, zero-based. */
-    int size;           /* Size of the row, excluding the null term. */
-    int rsize;          /* Size of the rendered row. */
-    char *chars;        /* Row content. */
-    char *render;       /* Row content "rendered" for screen (for TABs). */
-    unsigned char *hl;  /* Syntax highlight type for each character in render.*/
-    int hl_oc;          /* Row had open comment at end in last syntax highlight
+    int idx; /* Row index in the file, zero-based. */
+    int size; /* Size of the row, excluding the null term. */
+    int rsize; /* Size of the rendered row. */
+    char* chars; /* Row content. */
+    char* render; /* Row content "rendered" for screen (for TABs). */
+    unsigned char* hl; /* Syntax highlight type for each character in render.*/
+    int hl_oc; /* Row had open comment at end in last syntax highlight
                            check. */
 } erow;
 
-typedef struct part1_erow{
-	int part;
+typedef struct part1_erow {
+    int part;
 } part1_erow;
-typedef struct part1a_erow{
-	int part;
-	int part2;
+typedef struct part1a_erow {
+    int part;
+    int part2;
 } part1a_erow;
-typedef struct part1b_erow{
-	int part;
-	int part2;
-	int part3;
+typedef struct part1b_erow {
+    int part;
+    int part2;
+    int part3;
 } part1b_erow;
-typedef struct part1c_erow{
-	int part;
-	int part2;
-	int part3;
-	char * part4;
+typedef struct part1c_erow {
+    int part;
+    int part2;
+    int part3;
+    char* part4;
 } part1c_erow;
-typedef struct part1d_erow{
-	int part;
-	int part2;
-	int part3;
-	char * part4;
-	char * part5;
+typedef struct part1d_erow {
+    int part;
+    int part2;
+    int part3;
+    char* part4;
+    char* part5;
 } part1d_erow;
-typedef struct part1e_erow{
-	int part;
-	int part2;
-	int part3;
-	char * part4;
-	char * part5;
-	unsigned char * part6;
+typedef struct part1e_erow {
+    int part;
+    int part2;
+    int part3;
+    char* part4;
+    char* part5;
+    unsigned char* part6;
 } part1e_erow;
 
-typedef struct part2_erow{
-	char * part;
+typedef struct part2_erow {
+    char* part;
 } part2_erow;
-typedef struct part2a_erow{
-	char * part;
-	char * part2;
+typedef struct part2a_erow {
+    char* part;
+    char* part2;
 } part2a_erow;
 
-typedef struct part3_erow{
-	unsigned char * part;
+typedef struct part3_erow {
+    unsigned char* part;
 } part3_erow;
 
 struct editorSyntax {
-    char **filematch;
-    char **keywords;
+    char** filematch;
+    char** keywords;
     char singleline_comment_start[2];
     char multiline_comment_start[3];
     char multiline_comment_end[3];
@@ -118,33 +116,30 @@ struct editorSyntax {
 };
 
 /* C / C++ */
-char *C_HL_extensions[] = {".c",".cpp",NULL};
-char *C_HL_keywords[] = {
-        /* A few C / C++ keywords */
-        "switch","if","while","for","break","continue","return","else",
-        "struct","union","typedef","static","enum","class",
-        /* C types */
-        "int|","long|","double|","float|","char|","unsigned|","signed|",
-        "void|",NULL
+char* C_HL_extensions[] = { ".c", ".cpp", NULL };
+char* C_HL_keywords[] = {
+    /* A few C / C++ keywords */
+    "switch", "if", "while", "for", "break", "continue", "return", "else",
+    "struct", "union", "typedef", "static", "enum", "class",
+    /* C types */
+    "int|", "long|", "double|", "float|", "char|", "unsigned|", "signed|",
+    "void|", NULL
 };
 
-#define HL_HIGHLIGHT_STRINGS (1<<0)
-#define HL_HIGHLIGHT_NUMBERS (1<<1)
+#define HL_HIGHLIGHT_STRINGS (1 << 0)
+#define HL_HIGHLIGHT_NUMBERS (1 << 1)
 
 /* Here we define an array of syntax highlights by extensions, keywords,
  * comments delimiters and flags. */
 struct editorSyntax HLDB[] = {
-    {
-        /* C / C++ */
+    { /* C / C++ */
         C_HL_extensions,
         C_HL_keywords,
-        "//","/*","*/",
-        HL_HIGHLIGHT_STRINGS | HL_HIGHLIGHT_NUMBERS
-    }
+        "//", "/*", "*/",
+        HL_HIGHLIGHT_STRINGS | HL_HIGHLIGHT_NUMBERS }
 };
 
-#define HLDB_ENTRIES ((sizeof(HLDB))/(sizeof(HLDB[0])))
-
+#define HLDB_ENTRIES ((sizeof(HLDB)) / (sizeof(HLDB[0])))
 
 typedef struct SP SP;
 
@@ -160,7 +155,6 @@ struct SP {
 typedef struct SP Mem;
 typedef SP Mem2;
 
-
 int main()
 {
     plan(83);
@@ -236,42 +230,42 @@ int main()
     is_not_less(sizeof(FILE*), 8);
     is_not_less(sizeof(struct s), 8);
 
-	diag("erow from kilo editor");
-	is_not_less(sizeof(part1_erow ),  4);
+    diag("erow from kilo editor");
+    is_not_less(sizeof(part1_erow), 4);
 
-	is_not_less(sizeof(part1a_erow ),  8);
-	is_not_less(sizeof(part1b_erow ), 12);
-	is_not_less(sizeof(part1c_erow ), 24);
-	is_not_less(sizeof(part1d_erow ), 32);
-	is_not_less(sizeof(part1e_erow ), 40);
-	is_not_less(sizeof(erow        ), 48);
+    is_not_less(sizeof(part1a_erow), 8);
+    is_not_less(sizeof(part1b_erow), 12);
+    is_not_less(sizeof(part1c_erow), 24);
+    is_not_less(sizeof(part1d_erow), 32);
+    is_not_less(sizeof(part1e_erow), 40);
+    is_not_less(sizeof(erow), 48);
 
-	is_not_less(sizeof(part2_erow ),  8);
-	is_not_less(sizeof(part2a_erow),  16);
-	is_not_less(sizeof(part3_erow ),  8);
+    is_not_less(sizeof(part2_erow), 8);
+    is_not_less(sizeof(part2a_erow), 16);
+    is_not_less(sizeof(part3_erow), 8);
 
-	diag("HLDB");
-	is_not_less(sizeof(HLDB   ), 32);
-	is_not_less(sizeof(HLDB[0]), 32);
-	is_true(sizeof(HLDB) == sizeof(HLDB[0]));
-	is_eq((HLDB_ENTRIES) , 1);
+    diag("HLDB");
+    is_not_less(sizeof(HLDB), 32);
+    is_not_less(sizeof(HLDB[0]), 32);
+    is_true(sizeof(HLDB) == sizeof(HLDB[0]));
+    is_eq((HLDB_ENTRIES), 1);
 
-	diag("sqlite examples");
-	is_not_less(sizeof(union SP_UN), 4);
+    diag("sqlite examples");
+    is_not_less(sizeof(union SP_UN), 4);
     Mem m;
-	is_not_less(sizeof(m), 16);
+    is_not_less(sizeof(m), 16);
     is_not_less(sizeof(m), sizeof(double) + sizeof(char));
     (void)(m);
     Mem2 m2;
-	is_not_less(sizeof(m2), 16);
+    is_not_less(sizeof(m2), 16);
     is_not_less(sizeof(m2), sizeof(double) + sizeof(char));
     (void)(m2);
     SP s;
-	is_not_less(sizeof(s), 16);
+    is_not_less(sizeof(s), 16);
     is_not_less(sizeof(s), sizeof(double) + sizeof(char));
     (void)(s);
     union SP_UN sp;
-	is_not_less(sizeof(sp), 4);
+    is_not_less(sizeof(sp), 4);
     is_not_less(sizeof(sp), sizeof(int));
     (void)(sp);
 
diff --git a/tests/stdio.c b/tests/stdio.c
index f8eb061..dac9560 100644
--- a/tests/stdio.c
+++ b/tests/stdio.c
@@ -479,17 +479,15 @@ void test_snprintf()
     n = sprintf(buffer, "%d plus %d is %d", a, b, a + b);
     is_streq(buffer, "5 plus 3 is 8");
     is_eq(n, 13);
-	
-
 
     char status[80];
-	char * filename = "out.txt";
-	int numrows = 10;
-	int dirty = 1;
+    char* filename = "out.txt";
+    int numrows = 10;
+    int dirty = 1;
     int len = snprintf(status, sizeof(status), "%.20s - %d lines %s",
         filename, numrows, dirty ? "(modified)" : "");
-	printf("%s\n",status);
-	is_eq(len,29);
+    printf("%s\n", status);
+    is_eq(len, 29);
 }
 
 int PrintFError(const char* format, ...)
@@ -499,7 +497,7 @@ int PrintFError(const char* format, ...)
     va_start(args, format);
     int s = vsprintf(buffer, format, args);
     va_end(args);
-	printf("vsnprintf buffer: `%s`\n", buffer);
+    printf("vsnprintf buffer: `%s`\n", buffer);
     return s;
 }
 
@@ -516,7 +514,7 @@ int PrintFError2(const char* format, ...)
     va_start(args, format);
     int s = vsnprintf(buffer, 256, format, args);
     va_end(args);
-	printf("vsnprintf buffer: `%s`\n", buffer);
+    printf("vsnprintf buffer: `%s`\n", buffer);
     return s;
 }
 
@@ -524,8 +522,8 @@ void test_vsnprintf()
 {
     int s = PrintFError2("Success function '%s' %.2f", "vsprintf", 3.1415926);
     is_true(s == 19 + 8 + 5);
-	s = PrintFError2("HHELP %d",(int)(2));
-	is_true(s == 7);
+    s = PrintFError2("HHELP %d", (int)(2));
+    is_true(s == 7);
 }
 
 void test_eof()
@@ -557,36 +555,36 @@ void test_perror()
 
 void test_getline()
 {
-	{
+    {
         diag("getline: not empty file");
         FILE* pFile;
         pFile = fopen(test_file, "r");
         is_not_null(pFile);
-        
+
         size_t len;
         char* line = NULL;
         char** pnt = &line;
         size_t* l = &len;
         ssize_t pos = getline(pnt, l, pFile);
-		for (int i=0;i< pos;i++) {
-			printf("[%d] : `%d`\n", i, line[i]);
-		}
-		printf("pos [%d] == filesize [%d]\n", pos, filesize);
+        for (int i = 0; i < pos; i++) {
+            printf("[%d] : `%d`\n", i, line[i]);
+        }
+        printf("pos [%d] == filesize [%d]\n", pos, filesize);
         is_eq(pos, filesize);
-	}
-	{
+    }
+    {
         diag("getline: not empty file");
         FILE* pFile;
         pFile = fopen("./tests/empty.txt", "r");
         is_not_null(pFile);
-        
+
         size_t len;
         char* line = NULL;
         char** pnt = &line;
         size_t* l = &len;
         ssize_t pos = getline(pnt, l, pFile);
         is_eq(pos, -1);
-	}
+    }
 }
 
 void test_sscanf()
diff --git a/tests/stdlib.c b/tests/stdlib.c
index e02d9f5..0a692e9 100644
--- a/tests/stdlib.c
+++ b/tests/stdlib.c
@@ -166,13 +166,44 @@ void test_malloc5()
 
 void test_realloc()
 {
-    int size = 5;
-    void* v = realloc((char*)(NULL), size * size);
-    char* c = (char*)v;
-    for (int n = 0; n < size * size - 1; n++)
-        c[n] = n % 26 + 'a';
-    c[size * size - 1] = '\0';
-    printf("realloc: %s\n", c);
+    {
+        int size = 5;
+        void* v = realloc((char*)(NULL), size * size);
+        char* c = (char*)v;
+        for (int n = 0; n < size * size - 1; n++)
+            c[n] = n % 26 + 'a';
+        c[size * size - 1] = '\0';
+        printf("realloc: %s\n", c);
+    }
+    {
+        // from : https://www.geeksforgeeks.org/g-fact-66/
+       int* ptr = (int*)malloc(sizeof(int) * 2);
+       int* ptr_new;
+
+       *ptr = 10;
+       *(ptr + 1) = 20;
+
+       ptr_new = (int*)realloc(ptr, sizeof(int) * 3);
+       *(ptr_new + 2) = 30;
+
+       is_eq(*(ptr_new + 0), 10);
+       is_eq(*(ptr_new + 1), 20);
+       is_eq(*(ptr_new + 2), 30);
+   }
+   {
+       // from : https://www.tutorialspoint.com/c_standard_library/c_function_realloc.htm
+       char* str;
+
+       /* Initial memory allocation */
+       str = (char*)malloc(15);
+       strcpy(str, "tutorialspoint");
+       printf("String = %s\n", str);
+
+       /* Reallocating memory */
+       str = (char*)realloc(str, 25);
+       strcat(str, ".com");
+       printf("String = %s\n", str);
+   }
 }
 
 // calloc() works exactly the same as malloc() however the memory is zeroed out.
@@ -295,7 +326,7 @@ void test_atoi_post()
 //		// pointer checking: m + n - 1 - i
 //		return NULL;
 //	}
-//	
+//
 //	void test_my_memrchr()
 //	{
 //		unsigned char * word = "Hello my dear world";
@@ -307,7 +338,7 @@ void test_atoi_post()
 
 int main()
 {
-    plan(763);
+    plan(766);
 
     struct_with_define();
 
diff --git a/tests/struct.c b/tests/struct.c
index de87c4c..63dfe08 100644
--- a/tests/struct.c
+++ b/tests/struct.c
@@ -628,81 +628,83 @@ void pointer_arithm_in_struct()
 }
 
 typedef struct
-  {
-  enum { UADD = 0, UDEL = 1, UMOV = 2, VMOV = 3 } typet;
-  int * head;
-  int * tail;
-  }
-undo;
+{
+    enum { UADD = 0,
+        UDEL = 1,
+        UMOV = 2,
+        VMOV = 3 } typet;
+    int* head;
+    int* tail;
+} undo;
 
 void typedef_with_union()
 {
-	diag("typedef with union");
-	undo   u ;
-	(void)(u);
-	u.typet = UMOV;
-	is_eq(u.typet, UMOV);
-	(void)(u.typet);
-	int y = 53;
-	u.head = &y;
-	is_eq(*u.head, y);
-
-	undo u2;
-	u2.typet = VMOV;
-	int y2 = 42;
-	u2.head = &y2;
-	is_eq(u2.typet, VMOV);
-	is_eq(*u2.head, y2  );
-
-	undo * u3 = &u2;
-	is_eq((*u3).typet, VMOV);
-	is_eq(*(*u3).head, y2  );
-
-	(*u3).typet = u.typet;
-	is_eq((*u3).typet, UMOV);
-
-	int bd = UDEL;
-	(*u3).typet = bd;
-	is_eq((*u3).typet, UDEL);
+    diag("typedef with union");
+    undo u;
+    (void)(u);
+    u.typet = UMOV;
+    is_eq(u.typet, UMOV);
+    (void)(u.typet);
+    int y = 53;
+    u.head = &y;
+    is_eq(*u.head, y);
+
+    undo u2;
+    u2.typet = VMOV;
+    int y2 = 42;
+    u2.head = &y2;
+    is_eq(u2.typet, VMOV);
+    is_eq(*u2.head, y2);
+
+    undo* u3 = &u2;
+    is_eq((*u3).typet, VMOV);
+    is_eq(*(*u3).head, y2);
+
+    (*u3).typet = u.typet;
+    is_eq((*u3).typet, UMOV);
+
+    int bd = UDEL;
+    (*u3).typet = bd;
+    is_eq((*u3).typet, UDEL);
 }
 
 typedef union {
-  void *p;
-  int b;
+    void* p;
+    int b;
 } Value;
 
-#define TValuefields	Value value; int tt
+#define TValuefields \
+    Value value;     \
+    int tt
 
 typedef struct lua_TValue {
-  TValuefields;
+    TValuefields;
 } TValue;
 
-
-
 void typedef_struct_with_typedef_union()
 {
-	diag("typedef struct with typedef union");
-	Value v;
-	v.b = 42;
-	is_eq(v.b, 42);
-	TValue tv;
-	tv.value = v;
-	tv.tt    = 55;
-	is_eq(tv.value.b, 42);
-	is_eq(tv.tt     , 55);
-
-	TValue *ptv = &tv;
-	is_eq(ptv->value.b,42);
-	is_eq(ptv->tt     ,55);
-
-	double d = 45.0;
-	v.p = &d;
-	tv.value = v;
-	is_eq(*((double *)(v.p)), d);
-	is_eq(*((double *)(ptv->value.p)), d);
-	is_eq(ptv->tt, 55);
-
-	is_eq(*((double*)((&tv)->value.p)), d);
+    diag("typedef struct with typedef union");
+    Value v;
+    v.b = 42;
+    is_eq(v.b, 42);
+    TValue tv;
+    tv.value = v;
+    tv.tt = 55;
+    is_eq(tv.value.b, 42);
+    is_eq(tv.tt, 55);
+
+    TValue* ptv = &tv;
+    is_eq(ptv->value.b, 42);
+    is_eq(ptv->tt, 55);
+
+    double d = 45.0;
+    v.p = &d;
+    tv.value = v;
+    is_eq(*((double*)(v.p)), d);
+    is_eq(*((double*)(ptv->value.p)), d);
+    is_eq(ptv->tt, 55);
+
+    is_eq(*((double*)((&tv)->value.p)), d);
 }
 
 int add(int a, int b)
@@ -711,27 +713,27 @@ int add(int a, int b)
 }
 
 struct simon {
-	int (*f)(int, int);
-	long double g;
+    int (*f)(int, int);
+    long double g;
 };
 
 void test_struct_with_func()
 {
-	struct simon s1;
-	s1.f = add;
-	s1.g = 46.;
-	is_eq(s1.f(12,23), 12+23);
-	is_eq(s1.g       , 46.  );
-	struct simon * ps = &s1;
-	is_eq(ps->f(12,23), 12+23);
-	is_eq(ps->g       , 46.  );
-	struct simon as[2];
-	as[0] = s1;
-	as[1] = *ps;
-	is_eq(as[0].f(12,23), 12+23);
-	is_eq(as[0].g       , 46.  );
-	is_eq(as[1].f(12,23), 12+23);
-	is_eq(as[1].g       , 46.  );
+    struct simon s1;
+    s1.f = add;
+    s1.g = 46.;
+    is_eq(s1.f(12, 23), 12 + 23);
+    is_eq(s1.g, 46.);
+    struct simon* ps = &s1;
+    is_eq(ps->f(12, 23), 12 + 23);
+    is_eq(ps->g, 46.);
+    struct simon as[2];
+    as[0] = s1;
+    as[1] = *ps;
+    is_eq(as[0].f(12, 23), 12 + 23);
+    is_eq(as[0].g, 46.);
+    is_eq(as[1].f(12, 23), 12 + 23);
+    is_eq(as[1].g, 46.);
 }
 
 int main()
@@ -1138,9 +1140,9 @@ int main()
 
     struct_inside_union();
 
-	typedef_with_union();
-	typedef_struct_with_typedef_union();
-	test_struct_with_func();
+    typedef_with_union();
+    typedef_struct_with_typedef_union();
+    test_struct_with_func();
 
     done_testing();
 }
diff --git a/tests/ternary.c b/tests/ternary.c
index 49adc85..db55cb6 100644
--- a/tests/ternary.c
+++ b/tests/ternary.c
@@ -97,15 +97,15 @@ int main()
         is_eq(d, 12);
     }
 
-	diag("ternaty without middle");
-	{
-		int a = 42;
-		a = 19 > -9? : 23;
-		is_eq(a, 1);
-		a = 54;
-		a = 4 < -1? : 25;
-		is_eq(a, 25);
-	}
+    diag("ternaty without middle");
+    {
+        int a = 42;
+        a = 19 > -9 ?: 23;
+        is_eq(a, 1);
+        a = 54;
+        a = 4 < -1 ?: 25;
+        is_eq(a, 25);
+    }
 
     done_testing();
 }