Skip to content
Snippets Groups Projects
Select Git revision
  • f86cf205c49639b5ffc0bba75014dfa129d929b5
  • main default protected
  • dev protected
  • f-linkahead-rename
  • f-real-id
  • f-filesystem-import
  • f-filesystem-link
  • f-filesystem-directory
  • f-filesystem-core
  • f-filesystem-cleanup
  • f-filesystem-main
  • f-name
  • keep_changes
  • f-permission-checks-2
  • f-mysql8-tests
  • f-retrieve-history
  • t-distinct-parents
  • v8.1.0
  • v8.0.0
  • v7.0.2
  • v7.0.1
  • v7.0.0
  • v6.0.1
  • v6.0.0
  • v5.0.0
  • v4.1.0
  • v4.0.0
  • v3.0
  • v2.0.30
29 results

patch.sh

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    test_value.cpp 4.17 KiB
    /*
     *
     * This file is a part of the CaosDB Project.
     *
     * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
     * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU Affero General Public License as
     * published by the Free Software Foundation, either version 3 of the
     * License, or (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU Affero General Public License for more details.
     *
     * You should have received a copy of the GNU Affero General Public License
     * along with this program. If not, see <https://www.gnu.org/licenses/>.
     *
     */
    
    #include "caosdb/value.h"                   // for Value
    #include "caosdb/entity/v1alpha1/main.pb.h" // for AtomicDataType, DataType
    #include <algorithm>                        // for max
    #include <cmath>                            // for isnan
    #include <gtest/gtest-message.h>            // for Message
    #include <gtest/gtest-test-part.h>          // for TestPartResult, SuiteApi...
    #include <gtest/gtest_pred_impl.h>          // for AssertionResult, Test
    #include <initializer_list>                 // for initializer_list
    #include <string>                           // for string, basic_string
    #include <vector>                           // for vector
    
    namespace caosdb::entity {
    using ProtoEntity = caosdb::entity::v1alpha1::Entity;
    using ProtoParent = caosdb::entity::v1alpha1::Parent;
    using ProtoDataType = caosdb::entity::v1alpha1::DataType;
    using ProtoAtomicDataType = caosdb::entity::v1alpha1::AtomicDataType;
    
    TEST(test_value, test_null) {
      Value value;
      EXPECT_TRUE(value.IsNull());
      EXPECT_FALSE(value.IsDouble());
      EXPECT_FALSE(value.IsBool());
      EXPECT_FALSE(value.IsInteger());
      EXPECT_FALSE(value.IsString());
      EXPECT_FALSE(value.IsList());
    }
    
    TEST(test_value, test_string) {
      Value value(std::string("test"));
      EXPECT_FALSE(value.IsNull());
      EXPECT_TRUE(value.IsString());
      EXPECT_FALSE(value.IsDouble());
      EXPECT_FALSE(value.IsBool());
      EXPECT_FALSE(value.IsInteger());
    
      EXPECT_EQ(value.AsString(), "test");
    
      Value empty_string(std::string(""));
      EXPECT_FALSE(empty_string.IsNull());
      EXPECT_TRUE(empty_string.IsString());
      EXPECT_FALSE(empty_string.IsDouble());
      EXPECT_FALSE(empty_string.IsBool());
      EXPECT_FALSE(empty_string.IsInteger());
    
      EXPECT_EQ(empty_string.AsString(), "");
    
      // Test inequality
      Value string1("1");
      Value int1(1);
      EXPECT_FALSE(string1 == int1);
    }
    
    TEST(test_value, test_double) {
      Value value(2.26);
      EXPECT_FALSE(value.IsNull());
      EXPECT_FALSE(value.IsString());
      EXPECT_TRUE(value.IsDouble());
      EXPECT_FALSE(value.IsBool());
      EXPECT_FALSE(value.IsInteger());
    
      EXPECT_EQ(value.AsDouble(), 2.26);
    
      Value nan(std::sqrt(-1.0));
      EXPECT_FALSE(nan.IsNull());
      EXPECT_FALSE(nan.IsString());
      EXPECT_TRUE(nan.IsDouble());
      EXPECT_FALSE(nan.IsBool());
      EXPECT_FALSE(nan.IsInteger());
    
      EXPECT_TRUE(std::isnan(nan.AsDouble()));
    }
    
    TEST(test_value, test_integer) {
      Value value(1337);
      EXPECT_FALSE(value.IsNull());
      EXPECT_FALSE(value.IsString());
      EXPECT_FALSE(value.IsDouble());
      EXPECT_FALSE(value.IsBool());
      EXPECT_TRUE(value.IsInteger());
    
      EXPECT_EQ(value.AsInteger(), 1337);
    }
    
    TEST(test_value, test_boolean) {
      Value value(true);
      EXPECT_FALSE(value.IsNull());
      EXPECT_FALSE(value.IsString());
      EXPECT_FALSE(value.IsDouble());
      EXPECT_TRUE(value.IsBool());
      EXPECT_FALSE(value.IsInteger());
    
      EXPECT_EQ(value.AsBool(), true);
    }
    
    TEST(test_value, test_list) {
      std::vector<std::string> ids;
      for (std::string id : {"id0", "id1", "id2"}) {
        ids.push_back(id);
      }
      Value value(ids);
    
      EXPECT_FALSE(value.IsNull());
      EXPECT_TRUE(value.IsList());
      EXPECT_FALSE(value.IsString());
      EXPECT_FALSE(value.IsDouble());
      EXPECT_FALSE(value.IsBool());
      EXPECT_FALSE(value.IsInteger());
    
      auto list_value = value.AsList();
      int counter = 0;
      for (auto item : list_value) {
        EXPECT_EQ(item.IsString(), true);
        EXPECT_EQ(item.AsString(), "id" + std::to_string(counter++));
      }
    }
    } // namespace caosdb::entity