#!/usr/bin/env python
#
# Copyright 2020 GoPro Inc.
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from .cmp import CompareBase, get_test_decorator


class _CompareFloats(CompareBase):

    def __init__(self, func, tolerance=0.001, **func_kwargs):
        self._func = func
        self._func_kwargs = func_kwargs
        self._tolerance = tolerance

    @staticmethod
    def serialize(data):
        ret = ''
        for float_set in data:
            ret += '{}: {}\n'.format(float_set[0], ' '.join('%f' % f for f in float_set[1:]))
        return ret

    @staticmethod
    def deserialize(data):
        ret = []
        for line in data.splitlines():
            name, floats = line.split(':', 1)
            ret.append([name] + [float(f) for f in floats.split()])
        return ret

    def get_out_data(self, debug=False, debug_func=None):
        return self._func(**self._func_kwargs)

    def compare_data(self, test_name, ref_data, out_data):
        err = []
        for float_set_id, (ref_floats, out_floats) in enumerate(zip(ref_data, out_data)):
            name = ref_floats[0]
            if name != out_floats[0]:
                err.append('{} float sets {} have different names: "{}" vs "{}"'.format(
                    test_name, float_set_id, ref_floats[0], out_floats[0]))
            if len(ref_floats) != len(out_floats):
                err.append(f'{test_name}: number of floats is different '
                           f'(ref:{len(ref_floats)}, out:{len(out_floats)})')
                break
            for i, (f0, f1) in enumerate(zip(ref_floats[1:], out_floats[1:])):
                diff = abs(f0 - f1)
                if diff > self._tolerance:
                    err.append('{} diff too large for float {}[{}]: |{}-{}|={} (>{})'.format(
                        test_name, name, i, f0, f1, diff, self._tolerance))
        return err


test_floats = get_test_decorator(_CompareFloats)

@@ -2031,24 +2031,46 @@
 floats%5B0%5D))%0A
+                break%0A
             

break early in case of error in cmp_floats