# Copyright (c) 2021 - present / Neuralmagic, Inc. 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 os

import pytest
from torch.utils.data import Dataset

from sparseml.pytorch.datasets import DatasetRegistry, MNISTDataset


def _validate_mnist(dataset: Dataset):
    assert len(dataset) > 0
    item = dataset[0]
    assert isinstance(item, tuple)
    assert item[0].shape[0] == 1
    assert item[0].shape[1] == 28
    assert item[0].shape[2] == 28
    assert item[1] < 10


@pytest.mark.skipif(
    os.getenv("NM_ML_SKIP_PYTORCH_TESTS", False),
    reason="Skipping pytorch tests",
)
@pytest.mark.skipif(
    os.getenv("NM_ML_SKIP_DATASET_TESTS", False),
    reason="Skipping dataset tests",
)
def test_mnist():
    train_dataset = MNISTDataset(train=True)
    _validate_mnist(train_dataset)

    val_dataset = MNISTDataset(train=False)
    _validate_mnist(val_dataset)

    reg_dataset = DatasetRegistry.create("mnist", train=False)
    _validate_mnist(reg_dataset)
