Skip to content

Commit

Permalink
Merge pull request #128 from elliotchance/future/126-tap
Browse files Browse the repository at this point in the history
TAP tests. Fixes #126
  • Loading branch information
elliotchance authored Jun 3, 2017
2 parents 529e9e4 + f3cb486 commit 99cc6c1
Show file tree
Hide file tree
Showing 49 changed files with 1,565 additions and 776 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
/coverage.txt
/c2go
/.vscode
*.coverprofile
21 changes: 12 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ script:
#
# As in @rodrigocorsi2 comment above (using full path to grep due to
# 'grep -n' alias).
export PKGS=$(go list ./... | grep -v c2go/build | /usr/bin/grep -v /vendor/)
export PKGS=$(go list ./... | grep -v c2go/build | grep -v /vendor/)
# Make comma-separated.
export PKGS_DELIM=$(echo "$PKGS" | paste -sd "," -)
Expand All @@ -60,13 +60,11 @@ script:
# so that all the test names are printed. It's also important that the
# covermode be set to "count" so that the coverage profiles can be merged
# correctly together with gocovmerge.
#
# Exit code 123 will be returned if any of the tests fail.
rm -f /tmp/out.txt
go list -f '{{if or (len .TestGoFiles) (len .XTestGoFiles)}}go test -v -tags=integration -race -covermode count -coverprofile {{.Name}}_{{len .Imports}}_{{len .Deps}}.coverprofile -coverpkg $PKGS_DELIM {{.ImportPath}}{{end}} >> /tmp/out.txt' $PKGS | xargs -I {} bash -c {}
if [ $? -ne 0 ]; then
# Print out the failures (removing a lot of the noise).
cat /tmp/out.txt | grep -v -- "--- PASS" | grep -v -- "=== RUN"
exit 1
fi
go list -f 'go test -v -tags=integration -race -covermode count -coverprofile {{.Name}}.coverprofile -coverpkg $PKGS_DELIM {{.ImportPath}}' $PKGS |
xargs -I{} bash -c '{}'
# Merge coverage profiles.
COVERAGE_FILES=`ls -1 *.coverprofile 2>/dev/null | wc -l`
Expand All @@ -77,19 +75,24 @@ script:
# Print stats
echo "Unit tests: " $(grep "=== RUN" /tmp/out.txt | wc -l)
echo "Integration tests: " $(grep "# Total tests" /tmp/out.txt | cut -c21-)
# These steps are from the README to verify it can be installed and run as
# documented.
- cd /tmp
- export C2GO=$GOPATH/src/github.com/elliotchance/c2go
- c2go transpile $C2GO/tests/prime.c
- c2go transpile $C2GO/examples/prime.c
- echo "47" | go run prime.go
- if [ $(c2go -v | wc -l) -ne 1 ]; then exit 1; fi
- if [ $(cat prime.go | wc -l) -eq 0 ]; then exit 1; fi
- if [ $(c2go ast $C2GO/tests/prime.c | wc -l) -eq 0 ]; then exit 1; fi
- if [ $(c2go ast $C2GO/examples/prime.c | wc -l) -eq 0 ]; then exit 1; fi

# Revert the cwd for any cleanup commands.
- cd -

after_success:
- include_cov=coverage.txt bash <(curl -s https://codecov.io/bash)

after_failure:
# Print out the failures (removing a lot of the noise).
- cat /tmp/out.txt | grep -v -- "--- PASS" | grep -v -- "=== RUN"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ c2go transpile myfile.c

The `c2go` program processes a single C file and outputs the translated code
in Go. Let's use an included example,
[prime.c](https://github.com/elliotchance/c2go/blob/master/tests/math/prime.c):
[prime.c](https://github.com/elliotchance/c2go/blob/master/examples/prime.c):

```c
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions darwin/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/elliotchance/c2go/noarch"
)

func BuiltinExpect(a, b int) bool {
return a != b
func BuiltinExpect(a, b int) int {
return noarch.BoolToInt(a != b)
}

func AssertRtn(functionName, filePath []byte, lineNumber int, expression []byte) bool {
Expand Down
4 changes: 3 additions & 1 deletion darwin/ctype.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func IsType(_c Darwin_ct_rune_t, _f uint32) uint32 {
return 1
}

if _f&CTYPE_G != 0 && unicode.IsGraphic(rune(_c)) {
// The IsSpace check is required becuase Go treats spaces as graphic
// characters, which C does not.
if _f&CTYPE_G != 0 && unicode.IsGraphic(rune(_c)) && !unicode.IsSpace(rune(_c)) {
return 1
}

Expand Down
4 changes: 4 additions & 0 deletions darwin/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ func SincospifStret(x float32) Float2 {
func SincospiStret(x float64) Double2 {
return Double2{0, 0}
}

func NaN(s []byte) float64 {
return math.NaN()
}
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion linux/ctype.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func generateCharacterTable() {
c |= ((1 << (6)) << 8)
}

if unicode.IsGraphic(rune(i)) {
// The IsSpace check is required becuase Go treats spaces as graphic
// characters, which C does not.
if unicode.IsGraphic(rune(i)) && !unicode.IsSpace(rune(i)) {
c |= ((1 << (7)) << 8)
}

Expand Down
19 changes: 19 additions & 0 deletions linux/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package linux

import (
"math"

"github.com/elliotchance/c2go/noarch"
)

func IsNanf(x float32) int {
return noarch.BoolToInt(math.IsNaN(float64(x)))
}

func IsInff(x float32) int {
return noarch.BoolToInt(math.IsInf(float64(x), 0))
}

func IsInf(x float64) int {
return noarch.BoolToInt(math.IsInf(x, 0))
}
56 changes: 52 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ package main

import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"

"regexp"

"github.com/elliotchance/c2go/util"
)

Expand Down Expand Up @@ -39,11 +44,22 @@ type programOut struct {
// go test -tags=integration -run=TestIntegrationScripts/tests/ctype/isalnum.c
//
func TestIntegrationScripts(t *testing.T) {
files, err := filepath.Glob("tests/*.c")
testFiles, err := filepath.Glob("tests/*.c")
if err != nil {
t.Fatal(err)
}

exampleFiles, err := filepath.Glob("examples/*.c")
if err != nil {
t.Fatal(err)
}

files := append(testFiles, exampleFiles...)

isVerbose := flag.CommandLine.Lookup("test.v").Value.String() == "true"

totalTapTests := 0

for _, file := range files {
// Create build folder
os.Mkdir("build/", os.ModePerm)
Expand Down Expand Up @@ -88,20 +104,52 @@ func TestIntegrationScripts(t *testing.T) {
err = cmd.Run()
goProgram.isZero = err == nil

// Check for special exit codes that signal that tests have failed.
if exitError, ok := err.(*exec.ExitError); ok {
exitStatus := exitError.Sys().(syscall.WaitStatus).ExitStatus()
if exitStatus == 101 || exitStatus == 102 {
t.Fatal(goProgram.stdout.String())
}
}

// Check if both exit codes are zero (or non-zero)
if cProgram.isZero != goProgram.isZero {
t.Fatalf("Expected: %t, Got: %t", cProgram.isZero, goProgram.isZero)
t.Fatalf("Exit statuses did not match.\n" +
util.ShowDiff(cProgram.stdout.String(),
goProgram.stdout.String()),
)
}

// Check stderr
if cProgram.stderr.String() != goProgram.stderr.String() {
t.Fatalf("Expected %q, Got: %q", cProgram.stderr.String(), goProgram.stderr.String())
t.Fatalf("Expected %q, Got: %q",
cProgram.stderr.String(),
goProgram.stderr.String())
}

// Check stdout
if cProgram.stdout.String() != goProgram.stdout.String() {
t.Fatalf(util.ShowDiff(cProgram.stdout.String(), goProgram.stdout.String()))
t.Fatalf(util.ShowDiff(cProgram.stdout.String(),
goProgram.stdout.String()))
}

// If this is not an example we will extact the number of tests run.
if strings.Index(file, "examples/") == -1 && isVerbose {
firstLine := strings.Split(goProgram.stdout.String(), "\n")[0]

matches := regexp.MustCompile(`1\.\.(\d+)`).
FindStringSubmatch(firstLine)
if len(matches) == 0 {
t.Fatalf("Test did not output tap: %s", file)
}

fmt.Printf("TAP: # %s: %s tests\n", file, matches[1])
totalTapTests += util.Atoi(matches[1])
}
})
}

if isVerbose {
fmt.Printf("TAP: # Total tests: %d\n", totalTapTests)
}
}
21 changes: 21 additions & 0 deletions noarch/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package noarch

import (
"math"
)

func Signbitf(x float32) int {
return BoolToInt(math.Signbit(float64(x)))
}

func Signbitd(x float64) int {
return BoolToInt(math.Signbit(x))
}

func Signbitl(x float64) int {
return BoolToInt(math.Signbit(x))
}

func IsNaN(x float64) int {
return BoolToInt(math.IsNaN(x))
}
4 changes: 1 addition & 3 deletions noarch/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ func Strlen(a []byte) int {
// TODO: The transpiler should have a syntax that means this proxy function
// does not need to exist.

// TODO: The real length of the string will only be up to the NULL
// character.
return len(a)
return len(NullTerminatedByteSlice(a))
}
8 changes: 8 additions & 0 deletions noarch/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ func NullTerminatedByteSlice(s []byte) string {

return string(newSlice)
}

func CStringIsNull(s []byte) bool {
if s == nil || len(s) < 1 {
return true
}

return s[0] == 0
}
24 changes: 20 additions & 4 deletions program/function_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ var builtInFunctionDefinitions = []string{
"int tolower(int) -> linux.ToLower",
"int toupper(int) -> linux.ToUpper",

// linux/math.h
"int __signbitf(float) -> noarch.Signbitf",
"int __signbit(double) -> noarch.Signbitd",
"int __signbitl(long double) -> noarch.Signbitl",
"int __isnanf(float) -> linux.IsNanf",
"int __isnan(double) -> noarch.IsNaN",
"int __isnanl(long double) -> noarch.IsNaN",
"int __isinff(float) -> linux.IsInff",
"int __isinf(double) -> linux.IsInf",
"int __isinfl(long double) -> linux.IsInf",

// darwin/math.h
"double __builtin_fabs(double) -> darwin.Fabs",
"float __builtin_fabsf(float) -> darwin.Fabsf",
Expand All @@ -87,6 +98,11 @@ var builtInFunctionDefinitions = []string{
"Float2 __sincospif_stret(float) -> darwin.SincospifStret",
"Double2 __sincos_stret(double) -> darwin.SincosStret",
"Float2 __sincosf_stret(float) -> darwin.SincosfStret",
"float __builtin_huge_valf() -> darwin.Inff",
"int __inline_signbitf(float) -> noarch.Signbitf",
"int __inline_signbitd(double) -> noarch.Signbitd",
"int __inline_signbitl(long double) -> noarch.Signbitl",
"double __builtin_nanf(const char*) -> darwin.NaN",

// linux/assert.h
"bool __assert_fail(const char*, const char*, unsigned int, const char*) -> linux.AssertFail",
Expand All @@ -99,18 +115,18 @@ var builtInFunctionDefinitions = []string{
"double acos(double) -> math.Acos",
"double asin(double) -> math.Asin",
"double atan(double) -> math.Atan",
"double atan2(double) -> math.Atan2",
"double atan2(double, double) -> math.Atan2",
"double ceil(double) -> math.Ceil",
"double cos(double) -> math.Cos",
"double cosh(double) -> math.Cosh",
"double exp(double) -> math.Exp",
"double fabs(double) -> math.Abs",
"double floor(double) -> math.Floor",
"double fmod(double) -> math.Mod",
"double ldexp(double) -> math.Ldexp",
"double fmod(double, double) -> math.Mod",
"double ldexp(double, int) -> math.Ldexp",
"double log(double) -> math.Log",
"double log10(double) -> math.Log10",
"double pow(double) -> math.Pow",
"double pow(double, double) -> math.Pow",
"double sin(double) -> math.Sin",
"double sinh(double) -> math.Sinh",
"double sqrt(double) -> math.Sqrt",
Expand Down
5 changes: 5 additions & 0 deletions program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type Program struct {
// transpiling the AST. These messages, which are code comments, are
// appended to the very top of the output file. See AddMessage().
messages []string

// A map of all the global variables (variables that exist outside of a
// function) and their types.
GlobalVariables map[string]string
}

// NewProgram creates a new blank program.
Expand All @@ -65,6 +69,7 @@ func NewProgram() *Program {
Structs: make(map[string]*Struct),
Verbose: false,
messages: []string{},
GlobalVariables: map[string]string{},
}
}

Expand Down
17 changes: 11 additions & 6 deletions tests/argv.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// This file contains tests for the system arguments (argv).

#include <stdio.h>
#include "tests.h"

int main(int argc, char *argv[])
int main(int argc, const char **argv)
{
int c;
plan(3);

printf("Number of command line arguments passed: %d\n", argc);
is_eq(argc, 3);

for (c = 1; c < argc; c++)
printf("%d. Command line argument passed is %s\n", c + 1, argv[c]);
// We cannot compare the zeroth argument becuase it will be different for C
// and Go.
// is_streq(argv[0], "build/go.out");

return 0;
is_streq(argv[1], "some");
is_streq(argv[2], "args");

done_testing();
}
Loading

0 comments on commit 99cc6c1

Please sign in to comment.