1. shuerhaaken
  2. libtaginfo

Commits

shuerhaaken  committed a8b1434

add image read/write test for wav and mp4 files

  • Participants
  • Parent commits 638162e
  • Branches master

Comments (0)

Files changed (4)

File ChangeLog

View file
  • Ignore whitespace
+commit 638162ef0984d6715e78d0e470a3e460fca2035f
+Author: Jörn Magens <shuerhaaken@googlemail.com>
+Date:   Thu Mar 21 09:58:27 2013 +0100
+
+    add image read/write test for ape and tta files
+
 commit 4329da4cfa8f353e55a4d4d6bfcaef0fcc584f95
 Author: Jörn Magens <shuerhaaken@googlemail.com>
 Date:   Thu Mar 21 09:51:04 2013 +0100

File tests/Makefile.am

View file
  • Ignore whitespace
     write_image_ape/test \
     write_image_flac/test \
     write_image_mp3/test \
+    write_image_mp4/test \
     write_image_mpc/test \
     write_image_ogg/test \
     write_image_tta/test \
+    write_image_wav/test \
     write_image_wma/test
 
 TESTS = $(check_PROGRAMS)
 write_image_mp3_test_SOURCES    = write_image_mp3/main.cc
 write_image_mp3_test_LDADD      = $(taginfo_ldadd)
 
+write_image_mp4_test_SOURCES    = write_image_mp4/main.cc
+write_image_mp4_test_LDADD      = $(taginfo_ldadd)
+
 write_image_mpc_test_SOURCES    = write_image_mpc/main.cc
 write_image_mpc_test_LDADD      = $(taginfo_ldadd)
 
 write_image_tta_test_SOURCES    = write_image_tta/main.cc
 write_image_tta_test_LDADD      = $(taginfo_ldadd)
 
+write_image_wav_test_SOURCES    = write_image_wav/main.cc
+write_image_wav_test_LDADD      = $(taginfo_ldadd)
+
 write_image_wma_test_SOURCES    = write_image_wma/main.cc
 write_image_wma_test_LDADD      = $(taginfo_ldadd)
 

File tests/write_image_mp4/main.cc

View file
  • Ignore whitespace
+#include "taginfo.h"
+#include <stdio.h>
+#include <iostream>
+#include <fstream>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+using namespace TagInfo;
+//using namespace std;
+
+void clean_up(char* map, int size, int fd) {
+    if(munmap(map, size) == -1) {
+        perror("Error un-mmapping the file");
+    }
+    if(fd != 0)
+        close(fd);
+}
+
+int main( void ) {
+    Info * info;
+    std::string val   = TESTDIR "samples/sample.m4a";
+    std::string image = TESTDIR "samples/test.jpg";
+    
+    //std::cout << std::endl << "val: " << val << std::endl;
+    std::string target = "/tmp/out_01.m4a";
+    
+    std::ifstream  src(val.c_str());
+    std::ofstream  dst(target.c_str());
+    dst << src.rdbuf();
+    
+    char* data;
+    int data_length;
+    ImageType image_type = IMAGE_TYPE_JPEG;
+    
+    struct stat filestatus;
+    stat( image.c_str(), &filestatus );
+    //cout << filestatus.st_size << " bytes\n";
+    data_length = filestatus.st_size;
+    int fd;
+    
+    fd = open(image.c_str(), O_RDONLY);
+    if (fd == -1) {
+        perror("Error opening file for reading");
+        EXIT_FAILURE;
+    }
+    data = (char*)mmap(0, data_length, PROT_READ, MAP_SHARED, fd, 0);
+    if(data == MAP_FAILED) {
+        close(fd);
+        perror("Error mmapping the file");
+        EXIT_FAILURE;
+    }
+    
+    info = Info::create_tag_info(target);
+    if(info) {
+        if(!info->set_image(data, data_length, image_type)) {
+            clean_up(data, data_length, fd);
+            delete info;
+            return EXIT_FAILURE;
+        }
+        info->write();
+    }
+    delete info;
+    info = NULL;
+    
+    info = Info::create_tag_info(target);
+    char* read;
+    int read_length;
+    ImageType read_type;
+    if(info) {
+        if(info->read()) {
+            if(!info->get_image(read, read_length, read_type)) {
+                delete info;
+                if(!read || remove(target.c_str()) != 0 ) {
+                    clean_up(data, data_length, fd);
+                    return EXIT_FAILURE;
+                }
+            }
+            int i;
+            for(i = 0; i<data_length; i++) {
+                if(!read || data[i] != read[i]) {
+                    clean_up(data, data_length, fd);
+                    free(read);
+                    return EXIT_FAILURE;
+                }
+            }
+            clean_up(data, data_length, fd);
+            free(read);
+            if(remove(target.c_str()) != 0 )
+                return EXIT_FAILURE;
+            return EXIT_SUCCESS;
+        }
+        delete info;
+        clean_up(data, data_length, fd);
+        if(remove(target.c_str()) != 0 )
+            return EXIT_FAILURE;
+        return EXIT_FAILURE;
+    }
+    else {
+       delete info;
+        clean_up(data, data_length, fd);
+        if(remove(target.c_str()) != 0 )
+            return EXIT_FAILURE;
+        return EXIT_FAILURE;
+    }
+    return EXIT_FAILURE;
+}
+

File tests/write_image_wav/main.cc

View file
  • Ignore whitespace
+#include "taginfo.h"
+#include <stdio.h>
+#include <iostream>
+#include <fstream>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+using namespace TagInfo;
+//using namespace std;
+
+void clean_up(char* map, int size, int fd) {
+    if(munmap(map, size) == -1) {
+        perror("Error un-mmapping the file");
+    }
+    if(fd != 0)
+        close(fd);
+}
+
+int main( void ) {
+    Info * info;
+    std::string val   = TESTDIR "samples/sample.wav";
+    std::string image = TESTDIR "samples/test.jpg";
+    
+    //std::cout << std::endl << "val: " << val << std::endl;
+    std::string target = "/tmp/out_01.wav";
+    
+    std::ifstream  src(val.c_str());
+    std::ofstream  dst(target.c_str());
+    dst << src.rdbuf();
+    
+    char* data;
+    int data_length;
+    ImageType image_type = IMAGE_TYPE_JPEG;
+    
+    struct stat filestatus;
+    stat( image.c_str(), &filestatus );
+    //cout << filestatus.st_size << " bytes\n";
+    data_length = filestatus.st_size;
+    int fd;
+    
+    fd = open(image.c_str(), O_RDONLY);
+    if (fd == -1) {
+        perror("Error opening file for reading");
+        EXIT_FAILURE;
+    }
+    
+    data = (char*)mmap(0, data_length, PROT_READ, MAP_SHARED, fd, 0);
+    if(data == MAP_FAILED) {
+        close(fd);
+        perror("Error mmapping the file");
+        EXIT_FAILURE;
+    }
+    
+    info = Info::create_tag_info(target);
+    if(info) {
+        if(!info->set_image(data, data_length, image_type)) {
+            clean_up(data, data_length, fd);
+            delete info;
+            return EXIT_FAILURE;
+        }
+        info->write();
+    }
+    delete info;
+    info = NULL;
+    
+    info = Info::create_tag_info(target);
+    char* read;
+    int read_length;
+    ImageType read_type;
+    if(info) {
+        if(info->read()) {
+            if(!info->get_image(read, read_length, read_type)) {
+                delete info;
+                if(remove(target.c_str()) != 0 ) {
+                    clean_up(data, data_length, fd);
+                    return EXIT_FAILURE;
+                }
+            }
+            int i;
+            for(i = 0; i<data_length; i++) {
+                if(data[i] != read[i]) {
+                    clean_up(data, data_length, fd);
+                    free(read);
+                    return EXIT_FAILURE;
+                }
+            }
+            clean_up(data, data_length, fd);
+            free(read);
+            if(remove(target.c_str()) != 0 )
+                return EXIT_FAILURE;
+            return EXIT_SUCCESS;
+        }
+        delete info;
+        clean_up(data, data_length, fd);
+        if(remove(target.c_str()) != 0 )
+            return EXIT_FAILURE;
+        return EXIT_FAILURE;
+    }
+    else {
+       delete info;
+        clean_up(data, data_length, fd);
+        if(remove(target.c_str()) != 0 )
+            return EXIT_FAILURE;
+        return EXIT_FAILURE;
+    }
+    return EXIT_FAILURE;
+}
+