# Copyright 2021 The FastEstimator Authors. All Rights Reserved.
#
# Licensed 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.
# ==============================================================================
import unittest

import numpy as np
import torch

from fastestimator.architecture.pytorch import ResNet9


class TestResNet9(unittest.TestCase):
    def test_resnet9_default(self):
        model = ResNet9()
        inp = torch.Tensor(np.ones((1, 3, 32, 32)))
        output_shape = model(inp).detach().numpy().shape
        self.assertEqual(output_shape, (1, 10))

    def test_resnet9_specific_input_size_classes(self):
        model = ResNet9(input_size=(1, 16, 16), classes=3)
        inp = torch.Tensor(np.ones((1, 1, 16, 16)))
        output_shape = model(inp).detach().numpy().shape
        self.assertEqual(output_shape, (1, 3))

    def test_resnet9_check_input_size(self):
        with self.subTest("length not 3"):
            with self.assertRaises(ValueError):
                ResNet9._check_input_size((1, ))

        with self.subTest("width or height is smaller than 16"):
            with self.assertRaises(ValueError):
                ResNet9._check_input_size((1, 13, 16))

            with self.assertRaises(ValueError):
                ResNet9._check_input_size((1, 16, 1))

        with self.subTest("both are not smaller than 16"):
            ResNet9._check_input_size((1, 16, 16))
            ResNet9._check_input_size((32, 16, 100))
