| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 | 32 |
| 33 // This file has the unit tests for the IdAllocator class. | 33 // This file has the unit tests for the IdAllocator class. |
| 34 | 34 |
| 35 #include "tests/common/win/testing_common.h" | 35 #include "tests/common/win/testing_common.h" |
| 36 #include "command_buffer/client/cross/id_allocator.h" | 36 #include "command_buffer/client/cross/id_allocator.h" |
| 37 | 37 |
| 38 namespace o3d { | 38 namespace o3d { |
| 39 namespace command_buffer { | 39 namespace command_buffer { |
| 40 | 40 |
| 41 using command_buffer::ResourceID; | 41 using command_buffer::ResourceId; |
| 42 | 42 |
| 43 class IdAllocatorTest : public testing::Test { | 43 class IdAllocatorTest : public testing::Test { |
| 44 protected: | 44 protected: |
| 45 virtual void SetUp() {} | 45 virtual void SetUp() {} |
| 46 virtual void TearDown() {} | 46 virtual void TearDown() {} |
| 47 | 47 |
| 48 IdAllocator* id_allocator() { return &id_allocator_; } | 48 IdAllocator* id_allocator() { return &id_allocator_; } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 IdAllocator id_allocator_; | 51 IdAllocator id_allocator_; |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 // Checks basic functionality: AllocateID, FreeID, InUse. | 54 // Checks basic functionality: AllocateID, FreeID, InUse. |
| 55 TEST_F(IdAllocatorTest, TestBasic) { | 55 TEST_F(IdAllocatorTest, TestBasic) { |
| 56 IdAllocator *allocator = id_allocator(); | 56 IdAllocator *allocator = id_allocator(); |
| 57 // Check that resource 0 is not in use | 57 // Check that resource 0 is not in use |
| 58 EXPECT_FALSE(allocator->InUse(0)); | 58 EXPECT_FALSE(allocator->InUse(0)); |
| 59 | 59 |
| 60 // Allocate an ID, check that it's in use. | 60 // Allocate an ID, check that it's in use. |
| 61 ResourceID id1 = allocator->AllocateID(); | 61 ResourceId id1 = allocator->AllocateID(); |
| 62 EXPECT_TRUE(allocator->InUse(id1)); | 62 EXPECT_TRUE(allocator->InUse(id1)); |
| 63 | 63 |
| 64 // Allocate another ID, check that it's in use, and different from the first | 64 // Allocate another ID, check that it's in use, and different from the first |
| 65 // one. | 65 // one. |
| 66 ResourceID id2 = allocator->AllocateID(); | 66 ResourceId id2 = allocator->AllocateID(); |
| 67 EXPECT_TRUE(allocator->InUse(id2)); | 67 EXPECT_TRUE(allocator->InUse(id2)); |
| 68 EXPECT_NE(id1, id2); | 68 EXPECT_NE(id1, id2); |
| 69 | 69 |
| 70 // Free one of the IDs, check that it's not in use any more. | 70 // Free one of the IDs, check that it's not in use any more. |
| 71 allocator->FreeID(id1); | 71 allocator->FreeID(id1); |
| 72 EXPECT_FALSE(allocator->InUse(id1)); | 72 EXPECT_FALSE(allocator->InUse(id1)); |
| 73 | 73 |
| 74 // Frees the other ID, check that it's not in use any more. | 74 // Frees the other ID, check that it's not in use any more. |
| 75 allocator->FreeID(id2); | 75 allocator->FreeID(id2); |
| 76 EXPECT_FALSE(allocator->InUse(id2)); | 76 EXPECT_FALSE(allocator->InUse(id2)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 // Checks that the resource IDs are allocated conservatively, and re-used after | 79 // Checks that the resource IDs are allocated conservatively, and re-used after |
| 80 // being freed. | 80 // being freed. |
| 81 TEST_F(IdAllocatorTest, TestAdvanced) { | 81 TEST_F(IdAllocatorTest, TestAdvanced) { |
| 82 IdAllocator *allocator = id_allocator(); | 82 IdAllocator *allocator = id_allocator(); |
| 83 | 83 |
| 84 // Allocate a significant number of resources. | 84 // Allocate a significant number of resources. |
| 85 const int kNumResources = 100; | 85 const int kNumResources = 100; |
| 86 ResourceID ids[kNumResources]; | 86 ResourceId ids[kNumResources]; |
| 87 for (int i = 0; i < kNumResources; ++i) { | 87 for (int i = 0; i < kNumResources; ++i) { |
| 88 ids[i] = allocator->AllocateID(); | 88 ids[i] = allocator->AllocateID(); |
| 89 EXPECT_TRUE(allocator->InUse(ids[i])); | 89 EXPECT_TRUE(allocator->InUse(ids[i])); |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Check that the allocation is conservative with resource IDs, that is that | 92 // Check that the allocation is conservative with resource IDs, that is that |
| 93 // the resource IDs don't go over kNumResources - so that the service doesn't | 93 // the resource IDs don't go over kNumResources - so that the service doesn't |
| 94 // have to allocate too many internal structures when the resources are used. | 94 // have to allocate too many internal structures when the resources are used. |
| 95 for (int i = 0; i < kNumResources; ++i) { | 95 for (int i = 0; i < kNumResources; ++i) { |
| 96 EXPECT_GT(kNumResources, ids[i]); | 96 EXPECT_GT(kNumResources, ids[i]); |
| 97 } | 97 } |
| 98 | 98 |
| 99 // Check that the next resources are still free. | 99 // Check that the next resources are still free. |
| 100 for (int i = 0; i < kNumResources; ++i) { | 100 for (int i = 0; i < kNumResources; ++i) { |
| 101 EXPECT_FALSE(allocator->InUse(kNumResources + i)); | 101 EXPECT_FALSE(allocator->InUse(kNumResources + i)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // Check that a new allocation re-uses the resource we just freed. | 104 // Check that a new allocation re-uses the resource we just freed. |
| 105 ResourceID id1 = ids[kNumResources / 2]; | 105 ResourceId id1 = ids[kNumResources / 2]; |
| 106 allocator->FreeID(id1); | 106 allocator->FreeID(id1); |
| 107 EXPECT_FALSE(allocator->InUse(id1)); | 107 EXPECT_FALSE(allocator->InUse(id1)); |
| 108 ResourceID id2 = allocator->AllocateID(); | 108 ResourceId id2 = allocator->AllocateID(); |
| 109 EXPECT_TRUE(allocator->InUse(id2)); | 109 EXPECT_TRUE(allocator->InUse(id2)); |
| 110 EXPECT_EQ(id1, id2); | 110 EXPECT_EQ(id1, id2); |
| 111 } | 111 } |
| 112 | 112 |
| 113 } // namespace command_buffer | 113 } // namespace command_buffer |
| 114 } // namespace o3d | 114 } // namespace o3d |
| OLD | NEW |