diff --git a/cmd/puffs-test-c/main.go b/cmd/puffs-test-c/main.go index db735b15..a79a9d70 100644 --- a/cmd/puffs-test-c/main.go +++ b/cmd/puffs-test-c/main.go @@ -1,7 +1,7 @@ // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. -// puffs-test-c runs directories of C test programs. +// puffs-test-c runs C test programs. package main import ( @@ -11,8 +11,6 @@ import ( "os" "os/exec" "path/filepath" - "sort" - "strings" ) func main() { @@ -26,17 +24,11 @@ func main1() error { flag.Parse() failed := false for _, arg := range flag.Args() { - filenames, err := listDir(arg) + f, err := do(arg) if err != nil { return err } - for _, filename := range filenames { - f, err := do(arg, filename) - if err != nil { - return err - } - failed = failed || f - } + failed = failed || f } if failed { return fmt.Errorf("%s: some tests failed", os.Args[0]) @@ -44,28 +36,7 @@ func main1() error { return nil } -func listDir(dirname string) (filenames []string, err error) { - f, err := os.Open(dirname) - if err != nil { - return nil, err - } - defer f.Close() - - infos, err := f.Readdir(-1) - if err != nil { - return nil, err - } - for _, o := range infos { - name := o.Name() - if strings.HasSuffix(name, ".c") { - filenames = append(filenames, name) - } - } - sort.Strings(filenames) - return filenames, nil -} - -func do(dirname string, filename string) (failed bool, err error) { +func do(filename string) (failed bool, err error) { workDir, err := ioutil.TempDir("", "puffs-test-c") if err != nil { return false, err @@ -73,7 +44,7 @@ func do(dirname string, filename string) (failed bool, err error) { defer os.RemoveAll(workDir) for _, cc := range []string{"clang", "gcc"} { - in := filepath.Join(dirname, filename) + in := filename + ".c" out := filepath.Join(workDir, cc+".out") ccCmd := exec.Command(cc, "-std=c99", "-Wall", "-Werror", "-o", out, in) @@ -86,7 +57,7 @@ func do(dirname string, filename string) (failed bool, err error) { outCmd := exec.Command(out) outCmd.Stdout = os.Stdout outCmd.Stderr = os.Stderr - outCmd.Dir = dirname + outCmd.Dir = filepath.Dir(filename) if err := outCmd.Run(); err == nil { // No-op. } else if _, ok := err.(*exec.ExitError); ok { diff --git a/cmd/puffs/test.go b/cmd/puffs/test.go index 6583cda1..8f0f21b7 100644 --- a/cmd/puffs/test.go +++ b/cmd/puffs/test.go @@ -34,10 +34,10 @@ func doTest(puffsRoot string, args []string) error { if recursive { arg = arg[:len(arg)-4] } - arg = filepath.Join(puffsRoot, filepath.FromSlash(arg)) + qualifiedArg := filepath.Join(puffsRoot, filepath.FromSlash(arg)) // Ensure that we are testing the latest version of the generated code. - if err := gen(puffsRoot, arg, langs, recursive); err != nil { + if err := gen(puffsRoot, qualifiedArg, langs, recursive); err != nil { return err } @@ -55,7 +55,7 @@ func doTest(puffsRoot string, args []string) error { } func test(puffsRoot, dirname string, langs []string, recursive bool) (failed bool, err error) { - filenames, dirnames, err := listDir(dirname, recursive) + filenames, dirnames, err := listDir(filepath.Join(puffsRoot, filepath.FromSlash(dirname)), recursive) if err != nil { return false, err } @@ -79,16 +79,13 @@ func test(puffsRoot, dirname string, langs []string, recursive bool) (failed boo } func testDir(puffsRoot string, dirname string, langs []string) (failed bool, err error) { - packageName := filepath.Base(dirname) - if !validName(packageName) { + if packageName := filepath.Base(dirname); !validName(packageName) { return false, fmt.Errorf(`invalid package %q, not in [a-z0-9]+`, packageName) } for _, lang := range langs { command := "puffs-test-" + lang - testDirname := filepath.Join(puffsRoot, "test", lang, packageName) - fmt.Println("test: ", testDirname) - cmd := exec.Command(command, testDirname) + cmd := exec.Command(command, filepath.Join(puffsRoot, "test", lang, filepath.FromSlash(dirname))) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err == nil { diff --git a/test/c/gif/gif.c b/test/c/gif/gif.c deleted file mode 100644 index 0c538d28..00000000 --- a/test/c/gif/gif.c +++ /dev/null @@ -1,86 +0,0 @@ -// Use of this source code is governed by a BSD-style license that can be found -// in the LICENSE file. - -/* -To manually run this test: - -for cc in clang gcc; do - $cc -std=c99 -Wall -Werror gif.c && ./a.out - rm -f a.out -done - -Each edition should print "PASS", amongst other information, and exit(0). -*/ - -#include "../../../gen/c/gif.c" -#include "../testlib/testlib.c" - -const char* test_filename = "gif/gif.c"; - -#define BUFFER_SIZE (1024 * 1024) - -uint8_t global_got_buffer[BUFFER_SIZE]; -uint8_t global_want_buffer[BUFFER_SIZE]; -uint8_t global_src_buffer[BUFFER_SIZE]; - -void test_lzw_decode() { - test_funcname = __func__; - puffs_base_buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE}; - puffs_base_buf1 want = {.ptr = global_want_buffer, .len = BUFFER_SIZE}; - puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE}; - - // The want .indexes file should be 19200 bytes long, as the image size is - // 160 * 120 pixels and there is 1 palette index byte per pixel. - if (!read_file(&want, "../../testdata/bricks-nodither.indexes")) { - goto cleanup0; - } - if (want.wi != 19200) { - FAIL("want size: got %d, want %d", (int)(want.wi), 19200); - goto cleanup0; - } - - // The src .giflzw file should be 13382 bytes long. - if (!read_file(&src, "../../testdata/bricks-nodither.giflzw")) { - goto cleanup0; - } - if (src.wi != 13382) { - FAIL("src size: got %d, want %d", (int)(src.wi), 13382); - goto cleanup0; - } - // The first byte in that file, the LZW literal width, should be 0x08. - uint8_t literal_width = src.ptr[0]; - if (literal_width != 0x08) { - FAIL("LZW literal width: got %d, want %d", (int)(src.ptr[0]), 0x08); - goto cleanup0; - } - src.ri++; - - puffs_gif_lzw_decoder dec; - puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0); - puffs_gif_lzw_decoder_set_literal_width(&dec, literal_width); - puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec, &got, &src); - if (status != puffs_gif_status_ok) { - FAIL("status: got %d, want %d", status, puffs_gif_status_ok); - goto cleanup1; - } - - if (!buf1s_equal(&got, &want)) { - goto cleanup1; - } - // As a sanity check, the first decoded byte should be 0xDC. - if (got.ptr[0] != 0xDC) { - FAIL("first decoded byte: got 0x%02x, want 0x%02x", (int)(got.ptr[0]), - 0xDC); - goto cleanup1; - } - -cleanup1: - puffs_gif_lzw_decoder_destructor(&dec); -cleanup0:; -} - -// The empty comments forces clang-format to place one element per line. -test tests[] = { - test_lzw_decode, // - NULL, // -}; diff --git a/test/c/gif/basic.c b/test/c/std/gif.c similarity index 72% rename from test/c/gif/basic.c rename to test/c/std/gif.c index 65b12d89..66ef332a 100644 --- a/test/c/gif/basic.c +++ b/test/c/std/gif.c @@ -5,7 +5,7 @@ To manually run this test: for cc in clang gcc; do - $cc -std=c99 -Wall -Werror basic.c && ./a.out + $cc -std=c99 -Wall -Werror gif.c && ./a.out rm -f a.out done @@ -15,7 +15,15 @@ Each edition should print "PASS", amongst other information, and exit(0). #include "../../../gen/c/gif.c" #include "../testlib/testlib.c" -const char* test_filename = "gif/basic.c"; +const char* test_filename = "std/gif.c"; + +#define BUFFER_SIZE (1024 * 1024) + +uint8_t global_got_buffer[BUFFER_SIZE]; +uint8_t global_want_buffer[BUFFER_SIZE]; +uint8_t global_src_buffer[BUFFER_SIZE]; + +// ---------------- Basic Tests void test_constructor_not_called() { test_funcname = __func__; @@ -178,8 +186,69 @@ cleanup0: puffs_gif_decoder_destructor(&dec); } +// ---------------- LZW Tests + +void test_lzw_decode() { + test_funcname = __func__; + puffs_base_buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE}; + puffs_base_buf1 want = {.ptr = global_want_buffer, .len = BUFFER_SIZE}; + puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE}; + + // The want .indexes file should be 19200 bytes long, as the image size is + // 160 * 120 pixels and there is 1 palette index byte per pixel. + if (!read_file(&want, "../../testdata/bricks-nodither.indexes")) { + goto cleanup0; + } + if (want.wi != 19200) { + FAIL("want size: got %d, want %d", (int)(want.wi), 19200); + goto cleanup0; + } + + // The src .giflzw file should be 13382 bytes long. + if (!read_file(&src, "../../testdata/bricks-nodither.giflzw")) { + goto cleanup0; + } + if (src.wi != 13382) { + FAIL("src size: got %d, want %d", (int)(src.wi), 13382); + goto cleanup0; + } + // The first byte in that file, the LZW literal width, should be 0x08. + uint8_t literal_width = src.ptr[0]; + if (literal_width != 0x08) { + FAIL("LZW literal width: got %d, want %d", (int)(src.ptr[0]), 0x08); + goto cleanup0; + } + src.ri++; + + puffs_gif_lzw_decoder dec; + puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0); + puffs_gif_lzw_decoder_set_literal_width(&dec, literal_width); + puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec, &got, &src); + if (status != puffs_gif_status_ok) { + FAIL("status: got %d, want %d", status, puffs_gif_status_ok); + goto cleanup1; + } + + if (!buf1s_equal(&got, &want)) { + goto cleanup1; + } + // As a sanity check, the first decoded byte should be 0xDC. + if (got.ptr[0] != 0xDC) { + FAIL("first decoded byte: got 0x%02x, want 0x%02x", (int)(got.ptr[0]), + 0xDC); + goto cleanup1; + } + +cleanup1: + puffs_gif_lzw_decoder_destructor(&dec); +cleanup0:; +} + +// ---------------- Manifest + // The empty comments forces clang-format to place one element per line. test tests[] = { + // Basic Tests test_constructor_not_called, // test_bad_argument_null, // test_bad_argument_out_of_range, // @@ -189,5 +258,8 @@ test tests[] = { test_status_is_error, // test_status_strings, // test_sub_struct_constructor, // - NULL, // + // LZW Tests + test_lzw_decode, // + // End + NULL, };