gcov仅为标题类报告错误结果

时间:2016-11-04 10:02:44

标签: c++ unit-testing code-coverage gcov header-only

之前已经提出了许多类似的问题:

仅举几例。然而,他们都没有帮助我解决自己的问题。

我正在使用 gcc版本4.8.4(Ubuntu 4.8.4-2ubuntu1~14.04.3)编译我的源代码

    public class RvHomeAdapter extends RecyclerView.Adapter<RvHomeAdapter.NewsHolder> {

    List<News> newsArrayList;
    private static Context context;
    private static long downloadReference;
    VideoRequestHandler videoRequestHandler;
    Picasso picassoInstance;

    public RvHomeAdapter(List<News> newsArrayList, Context context) {
        this.newsArrayList = newsArrayList;
        RvHomeAdapter.context = context;
      /*  videoRequestHandler = new VideoRequestHandler();
        picassoInstance = new Picasso.Builder(context.getApplicationContext())
                .addRequestHandler(videoRequestHandler)
                .build();*/
    }


    @Override
    public NewsHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_row, viewGroup, false);
        NewsHolder newsHolder = new NewsHolder(v);
        return newsHolder;
    }

    @Override
    public void onBindViewHolder(final NewsHolder holder, final int position) {


        if (newsArrayList.get(position).getType().equals("image")) {

            holder.btn_play.setBackgroundResource(R.drawable.image);

            try {
                Picasso.with(context)
                        .load(newsArrayList.get(position).getThumb_image())
                        .fit()
                        .into(holder.img_main);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (newsArrayList.get(position).getType().equals("video")) {

            try {

                /*try {
                    Picasso.with(context)
                            .load(newsArrayList.get(position).getThumb_image())
                            .fit()
                            .into(holder.img_main);
                } catch (Exception e) {
                    e.printStackTrace();
                }*/


                FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
                mmr.setDataSource(newsArrayList.get(position).getLink());
                mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
                mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
                Bitmap b = mmr.getFrameAtTime(2000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); // frame at 2 seconds
//                byte [] artwork = mmr.getEmbeddedPicture();

                mmr.release();

                holder.img_main.setImageBitmap(b);


            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }

            holder.btn_play.setBackgroundResource(R.drawable.video);
//            holder.img_main.setImageBitmap(null);


        }
//        Typeface font = Typeface.createFromAsset(context.getAssets(), "saumil_guj.ttf");
//        holder.tv_news.setTypeface(font);
        holder.tv_news.setText(newsArrayList.get(position).getTitle());


    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }


    public static Bitmap retriveVideoFrameFromVideo(String videoPath)
            throws Throwable {
        Bitmap bitmap = null;
        MediaMetadataRetriever mediaMetadataRetriever = null;
        try {
            mediaMetadataRetriever = new MediaMetadataRetriever();
            if (Build.VERSION.SDK_INT >= 14)
                mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
            else
                mediaMetadataRetriever.setDataSource(videoPath);
            //   mediaMetadataRetriever.setDataSource(videoPath);
            bitmap = mediaMetadataRetriever.getFrameAtTime();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Throwable(
                    "Exception in retriveVideoFrameFromVideo(String videoPath)"
                            + e.getMessage());

        } finally {
            if (mediaMetadataRetriever != null) {
                mediaMetadataRetriever.release();
            }
        }

        Log.e("", "retriveVideoFrameFromVideo: bitmap = " + bitmap);

        return bitmap;
    }


    @Override
    public int getItemCount() {
        return newsArrayList.size();
    }

    public class NewsHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView img_download, img_share, img_main;
        TextView tv_news;
        Button btn_play;
        private DownloadManager downloadManager;

        public NewsHolder(View itemView) {
            super(itemView);

            btn_play = (Button) itemView.findViewById(R.id.btn_play);
            img_download = (ImageView) itemView.findViewById(R.id.img_download);
            img_share = (ImageView) itemView.findViewById(R.id.img_share);
            tv_news = (TextView) itemView.findViewById(R.id.tv_news);
            img_main = (ImageView) itemView.findViewById(R.id.img_main);

            img_main.setImageDrawable(null);
            btn_play.setOnClickListener(this);
            img_download.setOnClickListener(this);
            img_share.setOnClickListener(this);
            img_main.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (v.getId() == img_download.getId()) {

                String link = newsArrayList.get(getAdapterPosition()).getLink();
                String FileType = link.substring(link.lastIndexOf(".") + 1);
                System.out.println(link.substring(link.lastIndexOf(".") + 1));


                if (newsArrayList.get(getAdapterPosition()).getType().equals("image")) {

                    File extStore = Environment.getExternalStorageDirectory();
                    File myFile = new File(extStore.getAbsolutePath() + "/Botad News/Images/" + newsArrayList.get(getAdapterPosition()).getTitle() + "." + FileType);

                    if (!myFile.exists()) {
                        if (ConnectivityReceiver.isConnected()) {
                            DownloadTask downloadTask = new DownloadTask(context);
                            downloadTask.execute(newsArrayList.get(getAdapterPosition()).getLink(), "" + newsArrayList.get(getAdapterPosition()).getTitle() + "." + FileType, newsArrayList.get(getAdapterPosition()).getType());
                        } else {
                            Toast.makeText(context, context.getString(R.string.please_connect_to_internet), Toast.LENGTH_SHORT).show();
                        }
                    } else {

                        new AlertDialog.Builder(context)
                                .setTitle(R.string.app_name)
                                .setMessage(R.string.file_already_exists)
                                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // continue with delete
                                    }
                                })
                                .setIcon(R.mipmap.ic_launcher)
                                .show();

//                        Toast.makeText(context, "File already exists", Toast.LENGTH_SHORT).show();
                    }

                } else {

                    File extStore = Environment.getExternalStorageDirectory();
                    File myFile = new File(extStore.getAbsolutePath() + "/Botad News/Videos/" + newsArrayList.get(getAdapterPosition()).getTitle() + "." + FileType);

                    if (!myFile.exists()) {
                        if (ConnectivityReceiver.isConnected()) {
                            DownloadTask downloadTask = new DownloadTask(context);
                            downloadTask.execute(newsArrayList.get(getAdapterPosition()).getLink(), "" + newsArrayList.get(getAdapterPosition()).getTitle() + "." + FileType, newsArrayList.get(getAdapterPosition()).getType());
                        } else {
                            Toast.makeText(context, context.getString(R.string.please_connect_to_internet), Toast.LENGTH_SHORT).show();
                        }
                    } else {

                        new AlertDialog.Builder(context)
                                .setTitle(R.string.app_name)
                                .setMessage("File already exists !")
                                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // continue with delete
                                    }
                                })
                                .setIcon(R.mipmap.ic_launcher)
                                .show();

//                        Toast.makeText(context, "File already exists", Toast.LENGTH_SHORT).show();
                    }

                }


            } else if (v.getId() == img_share.getId()) {


                if (newsArrayList.get(getAdapterPosition()).getType().equals("video")) {

                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_SEND);
//                    intent.setType("text/plain");
                    intent.setType("video/*");
                    intent.putExtra(Intent.EXTRA_TEXT, newsArrayList.get(getAdapterPosition()).getLink());
                    context.startActivity(Intent.createChooser(intent, "Share"));

                } else {

                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_SEND);
//                    intent.setType("text/plain");
                    intent.setType("image/*");
                    intent.putExtra(Intent.EXTRA_TEXT, newsArrayList.get(getAdapterPosition()).getLink());
                    context.startActivity(Intent.createChooser(intent, "Share"));

                }


            } else if (v.getId() == btn_play.getId()) {

                if (newsArrayList.get(getAdapterPosition()).getType().contains("image")) {
//                    Toast.makeText(v.getContext(), "button image", Toast.LENGTH_SHORT).show();
                    Bundle gameData = new Bundle();
                    gameData.putString("file", newsArrayList.get(getAdapterPosition()).getLink());

                    Intent intent = new Intent(context, ImageViewActivity.class);
                    intent.putExtras(gameData);
                    context.startActivity(intent);

                } else {
                    if (ConnectivityReceiver.isConnected()) {
                        Bundle gameData = new Bundle();
                        gameData.putString("file", newsArrayList.get(getAdapterPosition()).getLink());
//                    Toast.makeText(v.getContext(), "button video", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(context, VideoViewActivity.class);
                        intent.putExtras(gameData);
                        context.startActivity(intent);
                    } else {
                        Toast.makeText(context, context.getString(R.string.please_connect_to_internet), Toast.LENGTH_SHORT).show();
                    }
                }

            } else {
//                Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
            }
        }


    }


}

然后我在我的一个源文件上运行 gcov(Ubuntu 4.8.4-2ubuntu1~14.04.3)4.8.4

g++ -c -pipe -Wno-psabi -std=gnu++11 -Wextra -pedantic -fprofile-arcs -ftest-coverage --coverage -fno-inline -fno-inline-small-functions -fno-default-inline -O0 -fno-elide-constructors -g -Wall -W -o .obj/myclass.o unittest_myclass.cpp

此文件包含仅限标题类的单元测试。 gcov成功生成此头文件的覆盖率信息:

gcov unittest_myclass.cpp -o .obj/

但是, myclass.h.gcov 中的结果是错误的。对于大多数行,gcov报告他们不包含可执行代码。对于那些检测为可执行文件的人,它主要报告他们尚未执行。但我确信它们正在被执行(我在调试期间验证了它,某些单元测试由于错误而失败等)。没有涉及模板。 因此我想由于某种原因gcov无法检测到正确的执行。从上面的命令行参数中可以看出:优化已关闭,内联已关闭......

还有什么想法会导致这些问题吗?

1 个答案:

答案 0 :(得分:1)

因为看起来问题不在于我的班级只是标题,而是我用

打电话给gcov

gcov unittest_myclass.cpp -o .obj/

如果我用

运行它

gcov *.cpp -o .obj/

报告

File '../include/myclass.h'
Lines executed:95.56% of 248
Creating 'myclass.h.gcov'

并且生成的myclass.h.gcov文件看起来比我预期的要多得多。