Skip to content

Commit

Permalink
Merge pull request #2 from elliotchance/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
zoeyfyi authored Apr 16, 2017
2 parents 336ffbb + 8952a01 commit c7b9916
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 111 deletions.
8 changes: 0 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ script:
fi
done
# Now run the integration tests.
- |
if [ "$TRAVIS_OS_NAME" = "linux" ]; then
CLANG_BIN=clang-$CLANG bash run-tests.sh
else
bash run-tests.sh
fi
# These steps are from the README to verify it can be installed and run as
# documented.
- cd /tmp
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Elliot Chance

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
44 changes: 5 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,37 +106,8 @@ func main() {

# What Is Supported?

This table represents what is supported. If you see anything missing (there is a
lot missing!) please add it with a pull request.

| Function | Supported? | Notes |
| ------------- | ------------- | -------------------------- |
| **assert.h** | Yes | |
| assert | Yes | This is actually a macro. |
| **math.h** | Partly | All of the C99 functions. |
| acos | Yes | |
| asin | Yes | |
| atan | Yes | |
| atan2 | Yes | |
| ceil | Yes | |
| cos | Yes | |
| cosh | Yes | |
| exp | Yes | |
| fabs | Yes | |
| floor | Yes | |
| fmod | Yes | |
| ldexp | Yes | |
| log | Yes | |
| log10 | Yes | |
| pow | Yes | |
| sin | Yes | |
| sinh | Yes | |
| sqrt | Yes | |
| tan | Yes | |
| tanh | Yes | |
| **stdio.h** | Partly | |
| printf | Yes | |
| scanf | Yes | |
See the
[Project Progress](https://github.com/elliotchance/c2go/wiki/Project-Progress).

# How It Works

Expand Down Expand Up @@ -170,20 +141,15 @@ For each of those files:
4. Both binaries are executed and the output is compared. All C files will
contain some output so the results can be verified.

The test suite is run with
[run-tests.sh](https://github.com/elliotchance/c2go/blob/master/run-tests.sh).
The test suite is run with `go test`.

# Contributing

As I said it is still very early days (sorry for all the hacky Python). And
eventually the build chain can be converted to pure Go since we don't need any
clang APIs.

Contributing is done with pull requests. There is no help that is too small! :)

If you're looking for where to start I can suggest
[finding a simple C program](http://www.programmingsimplified.com/c-program-examples)
(like the other examples) that does not successful translate into Go and fixing
up the Python so that it does.
(like the other examples) that do not successfully translate into Go.

Or, if you don't want to do that you can submit it as an issue so that it can be
picked up by someone else.
10 changes: 9 additions & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package main

import "strings"

var Imports = []string{"fmt"}
var Imports []string

func init() {
initImports()
}

func addImport(importName string) {
for _, i := range Imports {
Expand All @@ -25,3 +29,7 @@ func importType(typeName string) string {

return typeName
}

func initImports() {
Imports = []string{"fmt"}
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func Start(args []string) string {

all += ")\n\n" + go_out.String()

// Reset the imports
initImports()

return all
}

Expand Down
79 changes: 72 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,87 @@
package main

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)

// This test exists for code coverage and does not actually test anything. The
// real tests are performed with run-tests.h.
//
// In the future it would be nice to combine them so that the files only have to
// be compiled once and we don't need the extra bash script.
var (
cPath = "build/a.out"
goPath = "build/go.out"
stdin = "7"
args = []string{"some", "args"}
)

type programOut struct {
stdout bytes.Buffer
stderr bytes.Buffer
isZero bool
}

// TestIntegrationScripts tests all programs in the tests directory
func TestIntegrationScripts(t *testing.T) {
files, err := filepath.Glob("tests/*/*.c")
if err != nil {
panic(err)
t.Fatal(err)
}

for _, file := range files {
Start([]string{file})
// Create build folder
os.Mkdir("build/", os.ModePerm)

t.Run(file, func(t *testing.T) {
cProgram := programOut{}
goProgram := programOut{}

// Compile C
err := exec.Command("clang", "-lm", "-o", cPath, file).Run()
if err != nil {
t.Fatal(err)
}

// Run C program
cmd := exec.Command(cPath, args...)
cmd.Stdin = strings.NewReader(stdin)
cmd.Stdout = &cProgram.stdout
cmd.Stderr = &cProgram.stderr
err = cmd.Run()
cProgram.isZero = err == nil

// Compile Go
goSrc := Start([]string{file})
ioutil.WriteFile("build/main.go", []byte(goSrc), os.ModePerm)
err = exec.Command("go", "build", "-o", goPath, "build/main.go").Run()
if err != nil {
t.Fatal(err)
}

// Run Go program
cmd = exec.Command(goPath, args...)
cmd.Stdin = strings.NewReader(stdin)
cmd.Stdout = &goProgram.stdout
cmd.Stderr = &goProgram.stderr
err = cmd.Run()
goProgram.isZero = err == nil

// 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)
}

// Check stderr
if 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("Expected %q, Got: %q", cProgram.stdout.String(), goProgram.stdout.String())
}
})
}
}
56 changes: 0 additions & 56 deletions run-tests.sh

This file was deleted.

0 comments on commit c7b9916

Please sign in to comment.