ronantakizawa/codereview-bench huggingface dataset:
Repository: opencv/opencv
File: modules/python/src2/cv2_convert.cpp (C++)

Code before:
template<>
bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info)
{
    if(!obj || obj == Py_None)
    {
        return true;
    }
    std::string str;

#if ((PY_VERSION_HEX >= 0x03060000) && !defined(Py_LIMITED_API)) || (Py_LIMITED_API >= 0x03060000)
    PyObject* path_obj = NULL;
    if (info.pathlike)
    {
        path_obj = PyOS_FSPath(obj);
        if (PyErr_Occurred())
        {
            failmsg("Expected '%s' to be a str or path-like object", info.name);
            return false;
        }
        obj = path_obj;
    }
#endif
    if (getUnicodeString(obj, str))
    {
        value = str;
        Py_XDECREF(path_obj);
        return true;
    }
    else
    {
        if (!PyErr_Occurred())
        {
#ifdef Py_LIMITED_API
            failmsg("Can't convert object to 'str' for '%s'", info.name);
#else
            failmsg("Can't convert object of type '%s' to 'str' for '%s'",
                    obj->ob_type->tp_name, info.name);
#endif
        }
        Py_XDECREF(path_obj);
    }
    return false;
}

template<>
PyObject* pyopencv_from(const String& value)
{
    return PyString_FromString(value.empty() ? "" : value.c_str());
}

#if CV_VERSION_MAJOR == 3

Review comment (bug):
`PyObject* path_obj = NULL;` is inside `#if` block. Build is broken for the case without limited api option.

Code after:
template<>
bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info)
{
    if(!obj || obj == Py_None)
    {
        return true;
    }
    std::string str;

#if ((PY_VERSION_HEX >= 0x03060000) && !defined(Py_LIMITED_API)) || (Py_LIMITED_API >= 0x03060000)
    PyObject* path_obj = NULL;
    if (info.pathlike)
    {
        path_obj = PyOS_FSPath(obj);
        if (PyErr_Occurred())
        {
            failmsg("Expected '%s' to be a str or path-like object", info.name);
            return false;
        }
        obj = path_obj;
    }
#endif

    bool result = false;
    if (getUnicodeString(obj, str))
    {
        value = str;
        result = true;
    }
    else
    {
        if (!PyErr_Occurred())
        {
#ifdef Py_LIMITED_API
            failmsg("Can't convert object to 'str' for '%s'", info.name);
#else
            failmsg("Can't convert object of type '%s' to 'str' for '%s'",
                    obj->ob_type->tp_name, info.name);
#endif
        }
    }

#if ((PY_VERSION_HEX >= 0x03060000) && !defined(Py_LIMITED_API)) || (Py_LIMITED_API >= 0x03060000)
    Py_XDECREF(path_obj);
#endif

    return result;
}

template<>
PyObject* pyopencv_from(const String& value)
Repository: opencv/opencv
File: modules/dnn/src/layers/randomnormallike_layer.cpp (C++)

Code before:
#include "../net_impl.hpp"
#include <cmath>

namespace cv
{
namespace dnn
{

/*
    RandomNormalLike layer, as defined in ONNX specification:
    https://onnx.ai/onnx/operators/onnx__RandomNormalLike.html

    Supported Opsets: 1-22
*/

namespace
{
    void fillRandomNormal(OutputArray out, float mean, float scale,
                          bool has_seed, float seed)
    {
        CV_Assert(out.isMat() || out.isUMat());
        const Scalar mean_s = Scalar::all(mean);
        const Scalar scale_s = Scalar::all(scale);

        if (has_seed)
        {
            uint64 seed_u64 = (uint64)std::llround((double)seed);
            if (!seed_u64)
                seed_u64 = 0x12345678ULL;

            RNG rng(seed_u64);
            if (out.isMat())
            {
                Mat& m = out.getMatRef();
                rng.fill(m, RNG::NORMAL, mean_s, scale_s);
            }
            else
            {
                UMat& u = out.getUMatRef();
                rng.fill(u, RNG::NORMAL, mean_s, scale_s);
            }
        }
        else
        {
            RNG& rng = theRNG();
            if (out.isMat())
            {
                Mat& m = out.getMatRef();
                rng.fill(m, RNG::NORMAL, mean_s, scale_s);
            }
            else

Review comment (refactor):
You can significantly reduce the amount of code:

```
uint64_t seed_u64 = (uint64_t)std::llsound((double)seed);
RNG local_rng(seed_u64 != 0 ? seed_u64 : 0x12345678ULL);
RNG& rng = has_seed ? local_rng : theRNG();

if (out.isMat()) {
    ...
} else {
   ...
}
```

no need to duplicate branches.

Code after:
#include "../net_impl.hpp"
#include <cmath>

namespace cv
{
namespace dnn
{

/*
    RandomNormalLike layer, as defined in ONNX specification:
    https://onnx.ai/onnx/operators/onnx__RandomNormalLike.html

    Supported Opsets: 1-22
*/

namespace
{
    void fillRandomNormal(OutputArray out, float mean, float scale,
                          bool has_seed, float seed)
    {
        CV_Assert(out.isMat() || out.isUMat());
        const Scalar mean_s = Scalar::all(mean);
        const Scalar scale_s = Scalar::all(scale);

        RNG local_rng;
        if (has_seed)
        {
            uint64 seed_u64 = (uint64)std::llround((double)seed);
            if (!seed_u64)
                seed_u64 = 0x12345678ULL;
            local_rng = RNG(seed_u64);
        }

        RNG& rng = has_seed ? local_rng : theRNG();

        if (out.isMat())
        {
            Mat& m = out.getMatRef();
            rng.fill(m, RNG::NORMAL, mean_s, scale_s);
        }
        else
        {
            UMat& u = out.getUMatRef();
            rng.fill(u, RNG::NORMAL, mean_s, scale_s);
        }
    }
}

class RandomNormalLikeLayerImpl CV_FINAL : public RandomNormalLikeLayer
{
public:
Repository: opencv/opencv
File: modules/core/src/mathfuncs.cpp (C++)

Code before:
            {
                // linear equation
                x0 = -a3/a2;
                n = 1;
            }
        }
        else
        {
            // quadratic equation
            double d = a2*a2 - 4*a1*a3;
            if( d >= 0 )
            {
                d = std::sqrt(d);
                double q1 = (-a2 + d) * 0.5;
                double q2 = (a2 + d) * -0.5;
                if( std::abs(q1) > std::abs(q2) )
                {
                    x0 = q1 / a1;
                    x1 = a3 / q1;
                }
                else
                {
                    x0 = q2 / a1;
                    x1 = a3 / q2;
                }
                n = d > 0 ? 2 : 1;
            }
        }
    }
    else
    {
        // cubic equation
        // Use NORMALIZED coefficients to prevent overflow/instability
        double a0 = a0_norm; // Already normalized
        double a1 = raw_a1 * scale;
        double a2 = raw_a2 * scale;
        double a3 = raw_a3 * scale;

        a0 = 1./a0;
        a1 *= a0;
        a2 *= a0;
        a3 *= a0;

        double Q = (a1 * a1 - 3 * a2) * (1./9);
        double R = (a1 * (2 * a1 * a1 - 9 * a2) + 27 * a3) * (1./54);
        double Qcubed = Q * Q * Q;

        double d = (a1 * a1 * (a2 * a2 - 4 * a1 * a3) + 2 * a2 * (9 * a1 * a3 - 2 * a2 * a2) - 27 * a3 * a3) * (1./108);

        if( d > 0 )
        {

Review comment (suggestion):
**Please, do not remove old comments.**

---

BTW, too much AI code / comments.
No idea if the approach is really valid or not.

Code after:
            if( d >= 0 )
            {
                d = std::sqrt(d);
                double q1 = (-a2 + d) * 0.5;
                double q2 = (a2 + d) * -0.5;
                if( std::abs(q1) > std::abs(q2) )
                {
                    x0 = q1 / a1;
                    x1 = a3 / q1;
                }
                else
                {
                    x0 = q2 / a1;
                    x1 = a3 / q2;
                }
                n = d > 0 ? 2 : 1;
            }
        }
    }
    else
    {
        // cubic equation
        // Normalize coefficients in-place to prevent overflow/instability
        double scale = 1.0 / max_coeff;
        a0 *= scale;
        a1 *= scale;
        a2 *= scale;
        a3 *= scale;

        a0 = 1./a0;
        a1 *= a0;
        a2 *= a0;
        a3 *= a0;

        double Q = (a1 * a1 - 3 * a2) * (1./9);
        double R = (a1 * (2 * a1 * a1 - 9 * a2) + 27 * a3) * (1./54);
        double Qcubed = Q * Q * Q;

        /*
          Here we expand expression `Qcubed - R * R` for `d` variable
          to reduce common terms `a1^6 / 729` and `-a1^4 * a2 / 81`
          and thus decrease rounding error (in case of quite big coefficients).

          And then we additionally group terms to further reduce rounding error.
        */
        double d = (a1 * a1 * (a2 * a2 - 4 * a1 * a3) + 2 * a2 * (9 * a1 * a3 - 2 * a2 * a2) - 27 * a3 * a3) * (1./108);

        if( d > 0 )
        {
            double theta = acos(R / sqrt(Qcubed));
            double sqrtQ = sqrt(Q);
Repository: opencv/opencv
File: modules/java/generator/src/java9/CleanableMat.java (Java)

Code before:
package org.opencv.core;

import java.lang.ref.Cleaner;

public abstract class CleanableMat {
    // A native memory cleaner for the OpenCV library
    public static Cleaner cleaner = Cleaner.create();

    protected void registerCleaner() {
        // The n_delete action must not refer to the object being registered. So, do not use nativeObj directly.
        long nativeObjCopy = nativeObj;
        cleaner.register(this, () -> n_delete(nativeObjCopy));
    }

    private static native void n_delete(long nativeObj);

    public long nativeObj;
}


Review comment (suggestion):
Have you considered making it a one-argument contstructor like

```java
ClenableBase(long nativeObjectAddress) {
    ...
}
```

Then the use-sites could be like

```java
class Mat extends CleanableBase {
    public Mat(Mat m, Range rowRange, Range colRange) {
        super(n_Mat(m.nativeObj, rowRange.start, rowRange.end, colRange.start, colRange.end));
}
```

Code after:
package org.opencv.core;

import java.lang.ref.Cleaner;

public abstract class CleanableMat {
    // A native memory cleaner for the OpenCV library
    public static Cleaner cleaner = Cleaner.create();

    protected CleanableMat(long obj) {
        if (obj == 0)
            throw new UnsupportedOperationException("Native object address is NULL");

        nativeObj = obj;

        // The n_delete action must not refer to the object being registered. So, do not use nativeObj directly.
        long nativeObjCopy = nativeObj;
        cleaner.register(this, () -> n_delete(nativeObjCopy));
    }

    private static native void n_delete(long nativeObj);

    public final long nativeObj;
}
Repository: opencv/opencv
File: modules/java/generator/src/java_classic/CleanableMat.java (Java)

Code before:
package org.opencv.core;

import java.lang.ref.Cleaner;

public abstract class CleanableMat {

    protected CleanableMat(long obj) {
        if (obj == 0)
            throw new UnsupportedOperationException("Native object address is NULL");

        nativeObj = obj;
    }

    @Override
    protected void finalize() throws Throwable {
        n_delete(nativeObj);
        super.finalize();
    }

    private static native void n_delete(long nativeObj);

    public long nativeObj;
}


Review comment (suggestion):
WDYT of `final`?
```suggestion
    public final long nativeObj;
```

Code after:
package org.opencv.core;

public abstract class CleanableMat {

    protected CleanableMat(long obj) {
        if (obj == 0)
            throw new UnsupportedOperationException("Native object address is NULL");

        nativeObj = obj;
    }

    @Override
    protected void finalize() throws Throwable {
        n_delete(nativeObj);
        super.finalize();
    }

    private static native void n_delete(long nativeObj);

    public final long nativeObj;
}
Repository: opencv/opencv
File: modules/java/generator/src/java9/CleanableMat.java (Java)

Code before:
package org.opencv.core;

import java.lang.ref.Cleaner;

public abstract class CleanableMat {
    // A native memory cleaner for the OpenCV library
    public static Cleaner cleaner = Cleaner.create();

    protected CleanableMat(long obj) {
        if (obj == 0)
            throw new UnsupportedOperationException("Native object address is NULL");

        nativeObj = obj;

        // The n_delete action must not refer to the object being registered. So, do not use nativeObj directly.
        long nativeObjCopy = nativeObj;
        cleaner.register(this, () -> n_delete(nativeObjCopy));
    }

    private static native void n_delete(long nativeObj);

    public long nativeObj;
}


Review comment (suggestion):
WDYT of `final`?
```suggestion
    public final long nativeObj;
```

Code after:
package org.opencv.core;

import java.lang.ref.Cleaner;

public abstract class CleanableMat {
    // A native memory cleaner for the OpenCV library
    public static Cleaner cleaner = Cleaner.create();

    protected CleanableMat(long obj) {
        if (obj == 0)
            throw new UnsupportedOperationException("Native object address is NULL");

        nativeObj = obj;

        // The n_delete action must not refer to the object being registered. So, do not use nativeObj directly.
        long nativeObjCopy = nativeObj;
        cleaner.register(this, () -> n_delete(nativeObjCopy));
    }

    private static native void n_delete(long nativeObj);

    public final long nativeObj;
}
Repository: opencv/opencv
File: modules/photo/src/hdr_common.cpp (C++)

Code before:
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
#include "opencv2/photo.hpp"
#include "hdr_common.hpp"

namespace cv
{

void checkImageDimensions(const std::vector<Mat>& images)
{
    CV_Assert(!images.empty());
    int width = images[0].cols;
    int height = images[0].rows;
    int type = images[0].type();

    for(size_t i = 0; i < images.size(); i++) {
        CV_Assert(images[i].cols == width && images[i].rows == height);
        CV_Assert(images[i].type() == type);
    }
}

Mat triangleWeights(int length)
{
    // hat function
    CV_Assert(length >= 2);
    Mat w(length, 1, CV_32F);
    int half   = length / 2;
    int maxVal = length - 1;
    float epsilon = 1e-6f;
    w.at<float>(0) = epsilon;
    w.at<float>(length - 1) = epsilon;
    for (int i = 1; i < length - 1; i++){
        w.at<float>(i) = (i < half)
            ? static_cast<float>(i)
            : static_cast<float>(maxVal - i);
    }
    return w;
}

Mat triangleWeights()
{
    return triangleWeights(LDR_SIZE);
}

Mat RobertsonWeights(int length)
{
    CV_Assert(length >= 1);

Review comment (suggestion):
Please use `int length = LDR_SIZE` as default value and drop the overload.

Code after:
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
#include "opencv2/photo.hpp"
#include "hdr_common.hpp"

namespace cv
{

void checkImageDimensions(const std::vector<Mat>& images)
{
    CV_Assert(!images.empty());
    int width = images[0].cols;
    int height = images[0].rows;
    int type = images[0].type();

    for(size_t i = 0; i < images.size(); i++) {
        CV_Assert(images[i].cols == width && images[i].rows == height);
        CV_Assert(images[i].type() == type);
    }
}

Mat triangleWeights(int length)
{
    // hat function
    CV_Assert(length >= 2);
    Mat w(length, 1, CV_32F);
    int half   = length / 2;
    int maxVal = length - 1;
    float epsilon = 1e-6f;
    w.at<float>(0) = epsilon;
    w.at<float>(length - 1) = epsilon;
    for (int i = 1; i < length - 1; i++){
        w.at<float>(i) = (i < half)
            ? static_cast<float>(i)
            : static_cast<float>(maxVal - i);
    }
    return w;
}

Mat RobertsonWeights(int length)
{
    CV_Assert(length >= 1);
    Mat weight(length, 1, CV_32FC3);
    float q = (length - 1) / 4.0f;
    float e4 = exp(4.f);
    float scale = e4/(e4 - 1.f);
    float shift = 1 / (1.f - e4);
Repository: opencv/opencv
File: modules/dnn/test/test_tflite_importer.cpp (C++)

Code before:
}

TEST_P(Test_TFLite, StridedSlice) {
    testLayer("strided_slice");
}

TEST_P(Test_TFLite, face_blendshapes)
{
    Mat inp = blobFromNPY(findDataFile("dnn/tflite/face_blendshapes_inp.npy"));
    testModel("face_blendshapes", inp);
}

TEST_P(Test_TFLite, maximum)
{
    Net net = readNetFromTFLite(
        findDataFile("dnn/tflite/maximum.tflite"));

    net.setPreferableBackend(backend);
    net.setPreferableTarget(target);

    Mat input_x = blobFromNPY(
        findDataFile("dnn/tflite/maximum_input_x.npy"));
    Mat input_y = blobFromNPY(
        findDataFile("dnn/tflite/maximum_input_y.npy"));

    net.setInput(input_x, "input_x");
    net.setInput(input_y, "input_y");

    Mat out = net.forward();

    Mat ref = blobFromNPY(
        findDataFile("dnn/tflite/maximum_output.npy"));

    normAssert(ref, out);
}

INSTANTIATE_TEST_CASE_P(/**/, Test_TFLite, dnnBackendsAndTargets());

}}  // namespace

#endif  // OPENCV_TEST_DNN_TFLITE


Review comment (bug):
```
[ RUN      ] Test_TFLite.maximum/0, where GetParam() = OCV/CPU
unknown file: Failure
C++ exception with description "OpenCV(4.13.0-dev) /build/precommit_linux64/4.x/opencv/modules/dnn/src/net_impl.cpp:1406: error: (-204:Requested object was not found) Requested blob "input_x" not found in function 'setInput'
" thrown in the test body.
```

Code after:
}

TEST_P(Test_TFLite, StridedSlice) {
    testLayer("strided_slice");
}

TEST_P(Test_TFLite, face_blendshapes)
{
    Mat inp = blobFromNPY(findDataFile("dnn/tflite/face_blendshapes_inp.npy"));
    testModel("face_blendshapes", inp);
}

TEST_P(Test_TFLite, maximum)
{
    Net net = readNetFromTFLite(findDataFile("dnn/tflite/maximum.tflite"));

    net.setPreferableBackend(backend);
    net.setPreferableTarget(target);

    Mat input_x = blobFromNPY(findDataFile("dnn/tflite/maximum_input_x.npy"));
    Mat input_y = blobFromNPY(findDataFile("dnn/tflite/maximum_input_y.npy"));

    net.setInput(input_x, "x");
    net.setInput(input_y, "y");

    Mat out = net.forward();
    Mat ref = blobFromNPY(findDataFile("dnn/tflite/maximum_output.npy"));

    double l1 = 1e-5;
    double lInf = 1e-4;

    if (target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_OPENCL_FP16)
    {
        l1 = 1e-3;
        lInf = 1e-3;
    }

    normAssert(ref, out, "", l1, lInf);
}

INSTANTIATE_TEST_CASE_P(/**/, Test_TFLite, dnnBackendsAndTargets());

}}  // namespace

#endif  // OPENCV_TEST_DNN_TFLITE
Repository: opencv/opencv
File: modules/dnn/test/test_tflite_importer.cpp (C++)

Code before:
    testLayer("strided_slice");
}

TEST_P(Test_TFLite, face_blendshapes)
{
    Mat inp = blobFromNPY(findDataFile("dnn/tflite/face_blendshapes_inp.npy"));
    testModel("face_blendshapes", inp);
}

TEST_P(Test_TFLite, maximum)
{
    Net net = readNetFromTFLite(findDataFile("dnn/tflite/maximum.tflite"));

    net.setPreferableBackend(backend);
    net.setPreferableTarget(target);

    Mat input_x = blobFromNPY(findDataFile("dnn/tflite/maximum_input_x.npy"));
    Mat input_y = blobFromNPY(findDataFile("dnn/tflite/maximum_input_y.npy"));

    net.setInput(input_x, "x");
    net.setInput(input_y, "y");

    Mat out = net.forward();

    Mat ref = blobFromNPY(findDataFile("dnn/tflite/maximum_output.npy"));
    normAssert(ref, out);
}

INSTANTIATE_TEST_CASE_P(/**/, Test_TFLite, dnnBackendsAndTargets());

}}  // namespace

#endif  // OPENCV_TEST_DNN_TFLITE


Review comment (bug):
Please use a higher threshold for FP16 target. CUDA_FP16 test failed:
```
[ RUN      ] Test_TFLite.maximum/1, where GetParam() = CUDA/CUDA_FP16
/home/ci/opencv/modules/dnn/test/test_common.impl.hpp:84: Failure
Expected: (normL1) <= (l1), actual: 0.000195324 vs 1e-05
  |ref| = 5.5
/home/ci/opencv/modules/dnn/test/test_common.impl.hpp:87: Failure
Expected: (normInf) <= (lInf), actual: 0.000781298 vs 0.0001
  |ref| = 5.5
[  FAILED  ] Test_TFLite.maximum/1, where GetParam() = CUDA/CUDA_FP16 (4 ms)
```

Code after:
    testLayer("strided_slice");
}

TEST_P(Test_TFLite, face_blendshapes)
{
    Mat inp = blobFromNPY(findDataFile("dnn/tflite/face_blendshapes_inp.npy"));
    testModel("face_blendshapes", inp);
}

TEST_P(Test_TFLite, maximum)
{
    Net net = readNetFromTFLite(findDataFile("dnn/tflite/maximum.tflite"));

    net.setPreferableBackend(backend);
    net.setPreferableTarget(target);

    Mat input_x = blobFromNPY(findDataFile("dnn/tflite/maximum_input_x.npy"));
    Mat input_y = blobFromNPY(findDataFile("dnn/tflite/maximum_input_y.npy"));

    net.setInput(input_x, "x");
    net.setInput(input_y, "y");

    Mat out = net.forward();
    Mat ref = blobFromNPY(findDataFile("dnn/tflite/maximum_output.npy"));

    double l1 = 1e-5;
    double lInf = 1e-4;

    if (target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_OPENCL_FP16)
    {
        l1 = 1e-3;
        lInf = 1e-3;
    }

    normAssert(ref, out, "", l1, lInf);
}

INSTANTIATE_TEST_CASE_P(/**/, Test_TFLite, dnnBackendsAndTargets());

}}  // namespace

#endif  // OPENCV_TEST_DNN_TFLITE
Repository: opencv/opencv
File: modules/core/test/test_mat.cpp (C++)

Code before:

    a.push_back(Mat::ones(2, 2, CV_32FC1));

    ASSERT_EQ(6, a.rows);

    for(int row=4; row<a.rows; row++) {

        for(int col=0; col<a.cols; col++) {

            ASSERT_FLOAT_EQ(1.f, a.at<float>(row, col));
        }
    }
}

TEST(Core_Mat, copyToConvertTo_Empty)
{
    cv::Mat A(0, 0, CV_16SC2), B, C;
    A.copyTo(B);
    ASSERT_EQ(A.type(), B.type());
    A.convertTo(C, CV_32SC2);
    ASSERT_EQ(C.type(), CV_32SC2);
}

// Regression test for https://github.com/opencv/opencv/issues/28343
// copyTo on empty fixed-type matrices should be a no-op and succeed
TEST(Core_Mat, copyTo_EmptyFixedType)
{
    cv::Mat_<double> a;
    cv::Mat_<double> b;
    ASSERT_TRUE(a.empty());
    EXPECT_NO_THROW(a.copyTo(b));
    EXPECT_TRUE(b.empty());
    // Verify type is still consistent after copyTo
    EXPECT_EQ(b.type(), CV_64F);
}

TEST(Core_Mat, copyNx1ToVector)
{
    cv::Mat_<uchar> src(5, 1);
    cv::Mat_<uchar> ref_dst8;
    cv::Mat_<ushort> ref_dst16;
    std::vector<uchar> dst8;
    std::vector<ushort> dst16;

    src << 1, 2, 3, 4, 5;

    src.copyTo(ref_dst8);
    src.copyTo(dst8);

    ASSERT_PRED_FORMAT2(cvtest::MatComparator(0, 0), ref_dst8, cv::Mat_<uchar>(dst8));


Review comment (suggestion):
```suggestion
TYPED_TEST(Core_Mat, copyTo_EmptyFixedType)
```
I would suggest using [`TYPED_TEST`](https://google.github.io/googletest/reference/testing.html#TYPED_TEST_SUITE) and testing against all supported scalar types, not only `double`.

Code after:

    a.push_back(Mat::ones(2, 2, CV_32FC1));

    ASSERT_EQ(6, a.rows);

    for(int row=4; row<a.rows; row++) {

        for(int col=0; col<a.cols; col++) {

            ASSERT_FLOAT_EQ(1.f, a.at<float>(row, col));
        }
    }
}

TEST(Core_Mat, copyToConvertTo_Empty)
{
    cv::Mat A(0, 0, CV_16SC2), B, C;
    A.copyTo(B);
    ASSERT_EQ(A.type(), B.type());
    A.convertTo(C, CV_32SC2);
    ASSERT_EQ(C.type(), CV_32SC2);
}

// Regression test for https://github.com/opencv/opencv/issues/28343
// copyTo on empty fixed-type matrices should be a no-op and succeed
template <typename T> class Core_Mat_copyTo : public testing::Test {};
TYPED_TEST_CASE_P(Core_Mat_copyTo);

TYPED_TEST_P(Core_Mat_copyTo, EmptyFixedType)
{
    cv::Mat_<TypeParam> a;
    cv::Mat_<TypeParam> b;
    EXPECT_NO_THROW(a.copyTo(b));
    EXPECT_TRUE(b.empty());
    // Verify type is still consistent after copyTo
    EXPECT_EQ(b.type(), cv::traits::Type<TypeParam>::value);
}

REGISTER_TYPED_TEST_CASE_P(Core_Mat_copyTo, EmptyFixedType);
typedef ::testing::Types<uchar, schar, ushort, short, int, float, double> AllMatDepths;
INSTANTIATE_TYPED_TEST_CASE_P(CopyToTest, Core_Mat_copyTo, AllMatDepths);

TEST(Core_Mat, copyNx1ToVector)
{
    cv::Mat_<uchar> src(5, 1);
    cv::Mat_<uchar> ref_dst8;
    cv::Mat_<ushort> ref_dst16;
    std::vector<uchar> dst8;
    std::vector<ushort> dst16;

    src << 1, 2, 3, 4, 5;