Github user jacksontj commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/653#discussion_r64931455
  
    --- Diff: iocore/hostdb/test_RefCountCache.cc ---
    @@ -0,0 +1,178 @@
    +#define _test_RefCountCache_cc
    +
    +#include <iostream>
    +#include "P_RefCountCache.h"
    +#include "I_EventSystem.h"
    +#include "ts/I_Layout.h"
    +#include "diags.i"
    +
    +struct ExampleStruct {
    +  int idx;
    +  int name_offset; // pointer addr to name
    +
    +  // Return the char* to the name (TODO: cleaner interface??)
    +  char *
    +  name()
    +  {
    +    return (char *)this + this->name_offset;
    +  }
    +};
    +
    +void
    +fillCache(RefCountCache<ExampleStruct> *cache, int start, int end)
    +{
    +  // TODO: name per?
    +  std::string name = "foobar";
    +  int allocSize = name.size() + 1;
    +
    +  for (int i = start; i < end; i++) {
    +    ExampleStruct *tmp = (ExampleStruct *)cache->alloc((uint64_t)i, 
allocSize)->iobuf;
    +
    +    tmp->idx = i;
    +    tmp->name_offset = sizeof(ExampleStruct);
    +    memcpy(tmp->name(), name.c_str(), name.size());
    +    // NULL terminate the string
    +    *(tmp->name() + name.size()) = '\0';
    +
    +    // Print out the struct we put in there
    +    // printf("New ExampleStruct%d idx=%d name=%s allocSize=%d\n", i, 
tmp->idx, name.c_str(), allocSize);
    +  }
    +  printf("Loading complete! Cache now has %d items.\n\n", 
cache->numItems());
    +}
    +
    +int
    +verifyCache(RefCountCache<ExampleStruct> *cache, int start, int end)
    +{
    +  // Re-query all the structs to make sure they are there and accurate
    +  for (int i = start; i < end; i++) {
    +    Ptr<RefCountCacheItem<ExampleStruct>> ccitem = cache->get(i);
    +    if (ccitem.get() == NULL) {
    +      continue;
    +    }
    +    ExampleStruct *tmp = (ExampleStruct *)ccitem->iobuf;
    +    if (tmp == NULL) {
    +      // printf("ExampleStruct %d missing, skipping\n", i);
    +      continue;
    +    }
    +    // printf("Get (%p) ExampleStruct%d idx=%d name=%s\n", tmp, i, 
tmp->idx, tmp->name());
    +
    +    // Check that idx is correct
    +    if (tmp->idx != i) {
    +      printf("IDX of ExampleStruct%d incorrect!\n", i);
    +      return 1; // TODO: spin over all?
    +    }
    +
    +    // check that the name is correct
    +    // if (strcmp(tmp->name, name.c_str())){
    +    //  printf("Name of ExampleStruct%d incorrect! %s %s\n", i, tmp->name, 
name.c_str());
    +    //  exit(1);
    +    //}
    +  }
    +  return 0;
    +}
    +
    +// TODO: check that the memory was actually free-d better
    +int
    +testRefcounting()
    +{
    +  int ret = 0;
    +
    +  RefCountCache<ExampleStruct> *cache = new 
RefCountCache<ExampleStruct>(4);
    +
    +  // Set an item in the cache
    +  ExampleStruct *tmp = (ExampleStruct *)cache->alloc((uint64_t)1)->iobuf;
    +  tmp->idx = 1;
    +
    +  // Grab a pointer to item 1
    +  Ptr<RefCountCacheItem<ExampleStruct>> ccitem = cache->get((uint64_t)1);
    +
    +  Ptr<RefCountCacheItem<ExampleStruct>> tmpAfter = cache->get((uint64_t)1);
    +
    +  // Delete a single item
    +  cache->del(1);
    +  // verify that it still isn't in there
    +  ret |= cache->get(1).get() != NULL;
    +  ret |= tmpAfter.get()->item()->idx != 1;
    +
    +  (ExampleStruct *)cache->alloc((uint64_t)2);
    +  cache->del((uint64_t)2);
    +
    +  return ret;
    +}
    +
    +int
    +main()
    --- End diff --
    
    Why? These are tests on the generic cache (not even using ATS` real 
structs), to me this seems to make more sense here-- since there are actual 
regression tests (albeit not enough-- TODO is marked above).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to