Re: [vdagent-win PATCH 4/4] imagetest: replace assert with printf

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 
> If one does not have gdb installed it's nicer
> to simply get the error message (and some
> debug messages).
> 

I don't really agree. The imagetest is supposed to be a test
utility so is used only during development.
Without a debug you still can see the source file and line
printed.
The cleanup/return code IMHO is not that helpful for this
kind of utilities which are supposed to succeed if not bug
are present.
Also would at least be good to have the errors on stderr
and not stdout.

> Signed-off-by: Uri Lublin <uril@xxxxxxxxxx>
> ---
>  vdagent/imagetest.cpp | 99
>  ++++++++++++++++++++++++++++++++++++++++++---------
>  1 file changed, 83 insertions(+), 16 deletions(-)
> 
> diff --git a/vdagent/imagetest.cpp b/vdagent/imagetest.cpp
> index 3ea244e..809bcc6 100644
> --- a/vdagent/imagetest.cpp
> +++ b/vdagent/imagetest.cpp
> @@ -19,30 +19,62 @@
>  #include "vdcommon.h"
>  #include "image.h"
>  #include "imagepng.h"
> -#include <assert.h>
>  #include <vector>
>  
>  int main(int argc, char **argv)
>  {
>      ImageCoder *coder = create_png_coder();
>  
> -    assert(coder);
> -    assert(argc > 1);
> +    if (!coder) {
> +        printf("failed to create a png decoder\n");
> +        return 1;
> +    }
> +
> +    if (argc < 2) {
> +        printf("Usage: %s <in-image> [<out-bmp> [<out-png>]]\n", argv[0]);
> +        return 1;
> +    }
> +
> +    printf("opening %s\n", argv[1]);
>  
>      // read all file into memory
>      FILE *f = fopen(argv[1], "rb");
> -    assert(f);
> -    assert(fseek(f, 0, SEEK_END) == 0);
> +    if (!f) {
> +        printf("Failed to open %s\n", argv[1]);
> +        return 1;
> +    }
> +
> +    if (fseek(f, 0, SEEK_END) != 0) {
> +        printf("Failed to seek to the end-of-file\n");
> +        fclose(f);
> +        return 1;
> +    }
> +
>      long len = ftell(f);
> -    assert(len > 0);
> -    assert(fseek(f, 0, SEEK_SET) == 0);
> +    if (fseek(f, 0, SEEK_SET) != 0) {
> +        printf("Failed to see to the start-of-file\n");
> +        fclose(f);
> +        return 1;
> +    }
> +    printf("sizeof %s is %ld\n", argv[1], len);
>  
>      std::vector<uint8_t> data(len);
> -    assert(fread(&data[0], 1, len, f) == (unsigned long) len);
> +    long bytes = fread(&data[0], 1, len, f);
> +    if (bytes != len) {
> +        printf("read only %ld bytes, expected %ld\n", bytes, len);
> +        fclose(f);
> +        return 1;
> +    }
>      fclose(f);
> +    printf("read %ld bytes\n", bytes);
>  
>      size_t dib_size = coder->get_dib_size(&data[0], len);
> -    assert(dib_size);
> +    if (dib_size == 0) {
> +        printf("unexpected dib_size of 0\n");
> +        return 1;
> +    }
> +
> +    printf("dib_size is %zd\n", dib_size);

Not sure %zd is portable enough. Should be C99 but not sure MinGW is using
a recent printf implementation.

>      std::vector<uint8_t> out(dib_size);
>      memset(&out[0], 0xcc, dib_size);
>      coder->get_dib_data(&out[0], &data[0], len);
> @@ -55,24 +87,59 @@ int main(int argc, char **argv)
>      BITMAPINFOHEADER& info(*(BITMAPINFOHEADER*)&out[0]);
>      head.bfOffBits = sizeof(head) + sizeof(BITMAPINFOHEADER) + 4 *
>      info.biClrUsed;
>  
> +    printf("creating out.bmp\n");
>      f = fopen(argc > 2 ? argv[2] : "out.bmp", "wb");
> -    assert(f);
> -    assert(fwrite(&head, 1, sizeof(head), f) == sizeof(head));
> -    assert(fwrite(&out[0], 1, dib_size, f) == dib_size);
> +    if (!f) {
> +        printf("failed to open out.bmp\n");
> +        return 1;
> +    }
> +    bytes = fwrite(&head, 1, sizeof(head), f);
> +    if (bytes != sizeof(head)) {
> +        printf("header: wrote %ld bytes, expected %ld\n", bytes,
> sizeof(head));
> +        fclose(f);
> +        return 1;
> +    }
> +    bytes = fwrite(&out[0], 1, dib_size, f);
> +    if (bytes != dib_size) {
> +        printf("data: wrote %ld bytes, expected %ld\n", bytes, dib_size);
> +        fclose(f);
> +        return 1;
> +    }
>      fclose(f);
> +    printf("done\n");
> +    printf("converting to png\n");
>  
>      // convert back to PNG
>      long png_size = 0;
>      uint8_t *png = coder->from_bitmap(*((BITMAPINFO*)&out[0]),
>      &out[sizeof(BITMAPINFOHEADER) + 4 * info.biClrUsed], png_size);
> -    assert(png && png_size > 0);
> +    if (!png || (png_size == 0)) {
> +        printf("failed to convert to png (%p) size(%ld)\n", png, png_size);
> +        return 1;
> +    }
> +
> +    printf("creating out.png\n");
> +    int ret = 0;
>  
>      f = fopen(argc > 3 ? argv[3] : "out.png", "wb");
> -    assert(f);
> -    assert(fwrite(png, 1, png_size, f) == (unsigned long) png_size);
> +    if (!f) {
> +        printf("failed to open out.png\n");
> +        ret = 1;
> +        goto end;
> +    }
> +    bytes = fwrite(png, 1, png_size, f);
> +    if (bytes != png_size) {
> +        printf("png: wrote %ld, expected %ld\n", bytes, png_size);
> +        ret = 1;
> +        goto end;
> +    }
> +    printf("done\n");
> +
> +end:
>      fclose(f);
> +    f = NULL;
>      free(png);
>      png = NULL;
>  
> -    return 0;
> +    return ret;
>  }
>  

Frediano
_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]